-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzification.py
70 lines (51 loc) · 1.72 KB
/
fuzzification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Implementation of data fuzzification
Author: Kai Zhang (www.kaizhang.us)
https://github.com/taokz
"""
import numpy as np
from fuzzyset import FuzzySet
from gaussian_mf import gaussmf
import math
class FuzzyData(object):
_data = None
_fuzzydata = None
_epistemic_values = None
_target = None
def __init__(self, data = None, target = None):
if data is not None:
self._data = data
self._target = target
def quantile_fuzzification(self):
# reference: Guevara et al., Cross product kernels for fuzzy set similarity, 2017 FUZZY-IEEE
grouped = self._data.groupby([self._target])
self._epistemic_values = grouped.transform(lambda x:
np.exp(-np.square(x - x.quantile(0.5))
/
(np.abs(x.quantile(0.75) - x.quantile(0.25)) / (
2 * np.sqrt(2 * np.log(2))) + 0.001) ** 2
))
# fill up the NA (which is caused by the equal quantiles)
# self._epistemic_values = self._epistemic_values.fillna(0)
# join data and epistemistic values
num_rows = self._epistemic_values.shape[0]
num_cols = self._epistemic_values.shape[1]
self._fuzzydata=np.asarray([[FuzzySet(elements = self._data.iloc[j, i],
md=self._epistemic_values.iloc[j, i])
for i in range(num_cols)]
for j in range(num_rows)])
# return self._fuzzydata
def get_fuzzydata(self):
return self._fuzzydata
def get_data(self):
return self._data
def get_epistemic_values(self):
return self._epistemic_values
def get_target(self):
return self._data[self._target]
def show_class(self):
# print all contents of the class
print("(data) \n", _data, "\n")
print("(fuzzydata) \n", _fuzzydata, "\n")
print("(epistemic_values) \n", _epistemic_values, "\n")
print("(target) \n", _target, "\n")