-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_consistency.py
150 lines (133 loc) · 4.49 KB
/
test_consistency.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import pandas as pd
import numpy as np
import math
def unit_test():
data = pd.read_csv('mushrooms.csv')
y = data['class']
y_name = y.name
y_vector = y.values
x = data.drop(['class'], axis=1)
x_colNames = x.columns.values
x_values = x.values
x_vectors = x_values.transpose()
ci = ConsistencyInfo(x_vectors, x_colNames, y_vector)
top_feature = ci.top_feature
y_uniq = ci.y_uniq
x_uniq = ci.x_uniq
'''
print(x_vectors[top_feature[2]])
unique, counts = np.unique(x_vectors[top_feature[2]], return_counts=True)
print(dict(zip(unique, counts)))
for y in y_uniq:
for x in x_uniq[top_feature[2]]:
i = 0
cnt = 0
for x_iter in x_vectors[top_feature[2]]:
if x_iter == x and y_vector[i] == y:
cnt += 1
i+=1
print('y: ' + str(y) + ', x:' + str(x) + ', cnt:' + str(cnt))
'''
print(ci.sorted_pairs)
class ConsistencyInfo():
def __init__(self, x_vectors, x_colNames, y_vector, tol=15):
self.tol = tol
self.x_vectors = x_vectors
self.x_colNames = x_colNames
self.y_vector = y_vector
self.y_uniq = np.unique(y_vector)
self.n = len(self.y_uniq)
self.y_share = 1/self.n
self._setVectorUniqueItems()
self._getAllProbas()
self._getAllConsistency()
self._getAllFeatureConsistency()
self._getTopFeature()
#print(self.x_uniq)
def _setVectorUniqueItems(self):
self.x_uniq = []
for i in self.x_vectors:
self.x_uniq.append(np.unique(i))
def _getAllProbas(self):
i = 0
self.x_probas = []
for vector in self.x_vectors:
self.x_probas.append([])
for attribute in self.x_uniq[i]:
res = self._getProba(vector, attribute)
self.x_probas[i].append(res)
i += 1
#print(self.x_probas)
def _getProba(self, vector, attribute):
probas = []
probaSum = 0
#print(attribute)
for y in self.y_uniq:
#print(y)
match = 0
cnt = 0
for x in vector:
if x == attribute and self.y_vector[cnt] == y:
match += 1
cnt += 1
proba = (match/cnt)*self.y_share
#print(proba)
probaSum += proba
probas.append(proba)
normalized = []
for i in probas:
normalized.append(i/probaSum)
#print(i/probaSum)
#print('--------------------------------')
return normalized
def _getAllConsistency(self):
i = 0
self.x_consistency = []
for proba in self.x_probas:
self.x_consistency.append([])
for x in proba:
res = self._getConsistency(x)
self.x_consistency[i].append(res)
i += 1
#print(self.x_consistency)
def _getConsistency(self, probas):
cs = []
for proba in probas:
if proba <= self.y_share:
c = 1 - ( 1/( 1+math.exp( -10*self.n*proba + 5 ) ) )
else:
c = 1/( 1+math.exp( (5*self.n*(-2*proba + 1) +5) ) )
cs.append(c)
p = 1
for c in cs:
p *= c
return p
def _getAllFeatureConsistency(self):
self.feature_consistency = []
for cs in self.x_consistency:
fc = self._getFeatureConsistency(cs)
self.feature_consistency.append(fc)
def _getFeatureConsistency(self, cs):
m = len(cs)
fc_mean = 0
fc_max = 0
fc_share = 1/m
for c in cs:
fc_mean += c/m
if c > fc_max:
fc_max = c
fc_score = ((fc_max + fc_mean)/2)*( (2*(fc_share**(1/self.tol))) - (2*(0.5**(1/self.tol))) + 1)
return fc_score
def _getTopFeature(self):
i = 0
self.pairs = []
self.top_feature = [0,'', 0]
for f in self.feature_consistency:
if f > self.top_feature[0]:
self.top_feature = [f, self.x_colNames[i], i]
self.pairs.append([f, self.x_colNames[i], i])
i += 1
self.sorted_pairs = sorted(self.pairs, key=lambda x: x[0])
#print(self.pairs)
#print(self.top_feature)
unit_test()