-
Notifications
You must be signed in to change notification settings - Fork 0
/
CausalityFeatureFunctions.py
373 lines (322 loc) · 14.6 KB
/
CausalityFeatureFunctions.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import numpy as np
from sklearn.base import BaseEstimator
from scipy.special import psi
from scipy.stats.stats import pearsonr
import pwling
import math
from fitBothDirDiscrete import fitBothDirDiscrete
from scipy.stats import skew, skewtest, kurtosis, kurtosistest, ks_2samp, kendalltau, linregress, normaltest
from sklearn.feature_extraction.text import CountVectorizer
class FeatureFunctions:
def __init__(self):
self.arrayMax = np.zeros(7831)
self.arrayMean = np.zeros(7831)
self.arrayBinary = np.zeros(7831)
self.arrayMaxRev = np.zeros(7831)
self.arrayMeanRev = np.zeros(7831)
self.arrayBinaryRev = np.zeros(7831)
self.boolMax = True
self.boolMean = True
self.boolBinary = True
self.boolVar = True
self.boolVarRev = True
self.boolMaxRev = True
self.boolMeanRev = True
self.boolBinaryRev = True
self.counter = 0
def ks(self, x,y):
return ks_2samp(x, y)[1]
def kdt(self, x, y):
return kendalltau(x, y)[1]
def lr(self, x, y):
return linregress(x, y)[3]
def nt(self, x):
return normaltest(x)[1]
def identity(self, x):
return x
def count_unique(self, x):
return len(set(x))
def autocorr(self, x):
return len(np.correlate(x, x, mode='full'))
def mean_autocorr(self, x):
return (1.0-(self.autocorr(x)+0.0)/(2*len(x)+0.0))*100.0
def normalized_entropy(self, x):
x = (x - np.mean(x)) / np.std(x)
x = np.sort(x)
hx = 0.0;
for i in range(len(x)-1):
delta = x[i+1] - x[i];
if delta != 0:
hx += np.log(np.abs(delta));
hx = hx / (len(x) - 1) + psi(len(x)) - psi(1);
return hx
def divideInBuckets(self, x, valuesArray, n):
division = (len(valuesArray) +0.0)/(0.0 + float(n))
return [ valuesArray[int(round(division * i)): int(round(division * (i + 1)))] for i in xrange(n) ]
def vertical_line_test_max(self, x, y):
if self.boolMax:
self.boolMax = False
self.counter = 0
self.counter = self.counter + 1
return self.arrayMax[self.counter-1]
def vertical_line_test_mean(self, x, y):
if self.boolMean:
self.boolMean = False
self.counter = 0
self.counter = self.counter + 1
return self.arrayMean[self.counter-1]
def fbdd(self, x, y, atype, btype):
result = fitBothDirDiscrete(x, False, y, False, 0.05, True)
print result
return result
def fbdd_rev(self, x, y, atype, btype):
result = fitBothDirDiscrete(x, False, y, False, 0.05, False)
print result
return result
def vertical_line_test_var(self, x, yaux):
if self.boolVar:
self.boolVar = False
self.counter = 0
if yaux.max() - yaux.min() == 0:
return 0
y = (yaux + 0.0 - yaux.min())/(yaux.max() + 0.0 -yaux.min())
uniqueValues = np.unique(x)
varianceFinal = []
if len(uniqueValues) > 500:
uniqueValuesPrima = self.divideInBuckets(x, uniqueValues, 100)
for bucket in uniqueValuesPrima:
generalPositions = []
for element in bucket:
positions = np.where(x == element)[0]
generalPositions.extend(positions)
if len(generalPositions) == 1:
varianceFinal.append(0)
continue
variance = y[generalPositions].var(ddof=1)
if not math.isnan(variance):
varianceFinal.append(variance)
else:
for element in uniqueValues:
positions = np.where(x == element)[0]
if len(positions) == 1:
varianceFinal.append(0)
continue
variance = y[positions].var(ddof=1)
if not math.isnan(variance):
varianceFinal.append(variance)
if len(varianceFinal) == 0 or len(varianceFinal) == 1:
return 0
vf = np.array(varianceFinal)
self.arrayMean[self.counter] = vf.mean()
maxResult = np.max(vf)
self.arrayMax[self.counter] = maxResult
if maxResult != 0:
self.arrayBinary[self.counter] = 1
else:
self.arrayBinary[self.counter] = 0
self.counter = self.counter + 1
result = vf.var()
if not math.isnan(result):
return result
return 0
def vertical_line_test_var_rev(self, yaux, x):
if self.boolVarRev:
self.boolVarRev = False
self.counter = 0
uniqueValues = np.unique(x)
if yaux.max() - yaux.min() == 0:
return 0
y = (yaux + 0.0 - yaux.min())/(yaux.max() + 0.0 -yaux.min())
varianceFinal = []
if len(uniqueValues) > 500:
uniqueValuesPrima = self.divideInBuckets(x, uniqueValues, 100)
for bucket in uniqueValuesPrima:
generalPositions = []
for element in bucket:
positions = np.where(x == element)[0]
generalPositions.extend(positions)
if len(generalPositions) == 1:
varianceFinal.append(0)
continue
variance = y[generalPositions].var(ddof=1)
if not math.isnan(variance):
varianceFinal.append(variance)
else:
for element in uniqueValues:
positions = np.where(x == element)[0]
if len(positions) == 1:
varianceFinal.append(0)
continue
variance = y[positions].var(ddof=1)
if not math.isnan(variance):
varianceFinal.append(variance)
if len(varianceFinal) == 0 or len(varianceFinal) == 1:
return 0
vf = np.array(varianceFinal)
self.arrayMeanRev[self.counter] = vf.mean()
maxResult = np.max(vf)
self.arrayMaxRev[self.counter] = maxResult
if maxResult != 0:
self.arrayBinaryRev[self.counter] = 1
else:
self.arrayBinaryRev[self.counter] = 0
self.counter = self.counter + 1
result = vf.var()
if not math.isnan(result):
return result
return 0
def vertical_line_test_binary(self, x, y):
if self.boolBinary:
self.boolBinary = False
self.counter = 0
self.counter = self.counter + 1
return self.arrayBinary[self.counter-1]
def vertical_line_test_max_rev(self, y, x):
if self.boolMaxRev:
self.boolMaxRev = False
self.counter = 0
self.counter = self.counter + 1
return self.arrayMaxRev[self.counter-1]
def vertical_line_test_mean_rev(self, y, x):
if self.boolMeanRev:
self.boolMeanRev = False
self.counter = 0
self.counter = self.counter + 1
return self.arrayMeanRev[self.counter-1]
def skewness(self, x):
return skewtest(x)[0]
def skewnessp(self, x):
return skewtest(x)[1]
def kurtosist(self, x):
return kurtosistest(x)[0]
def kurtosistp(self, x):
return kurtosistest(x)[1]
def kurt(self, x):
return kurtosis(x)
def vertical_line_test_binary_rev(self, y, x):
if self.boolBinaryRev:
self.boolBinaryRev = False
self.counter = 0
self.counter = self.counter + 1
return self.arrayBinaryRev[self.counter-1]
def entropy_difference(self, x, y):
return self.normalized_entropy(x) - self.normalized_entropy(y)
def correlation(self, x, y):
return pearsonr(x, y)[0]
def correlation_magnitude(self, x, y):
return abs(self.correlation(x, y))
def pwling1(self, x, y):
arrayToTest = np.array([x,y])
return pwling.doPwling(arrayToTest, 1)
def pwling2(self, x, y):
arrayToTest = np.array([x,y])
return pwling.doPwling(arrayToTest, 2)
def pwling3(self, x, y):
arrayToTest = np.array([x,y])
return pwling.doPwling(arrayToTest, 3)
def pwling4(self, x, y):
arrayToTest = np.array([x,y])
return pwling.doPwling(arrayToTest, 4)
def pwling5(self, x, y):
arrayToTest = np.array([x,y])
return pwling.doPwling(arrayToTest, 5)
ff = FeatureFunctions()
class FeatureMapper:
def __init__(self, features):
self.features = features
def fit(self, X, y=None):
for _, column_names, extractor in self.features:
extractor.fit(X[column_names], y)
def transform(self, X):
extracted = []
for feature_name, column_names, extractor in self.features:
print feature_name
fea = extractor.transform(X[column_names])
if hasattr(fea, "toarray"):
extracted.append(fea.toarray())
else:
extracted.append(fea)
if len(extracted) > 1:
return np.concatenate(extracted, axis=1)
else:
return extracted[0]
def fit_transform(self, X, y=None):
extracted = []
for feature_name, column_names, extractor in self.features:
print feature_name
fea = extractor.fit_transform(X[column_names], y)
if hasattr(fea, "toarray"):
extracted.append(fea.toarray())
else:
extracted.append(fea)
print "Concatenating...."
if len(extracted) > 1:
return np.concatenate(extracted, axis=1)
else:
return extracted[0]
class SimpleTransform(BaseEstimator):
def __init__(self, transformer=ff.identity):
self.transformer = transformer
def fit(self, X, y=None):
return self
def fit_transform(self, X, y=None):
return self.transform(X)
def transform(self, X, y=None):
return np.array([self.transformer(x) for x in X], ndmin=2).T
class MultiColumnTransform(BaseEstimator):
def __init__(self, transformer):
self.transformer = transformer
def fit(self, X, y=None):
return self
def fit_transform(self, X, y=None):
return self.transform(X)
def transform(self, X, y=None):
return np.array([self.transformer(*x[1]) for x in X.iterrows()], ndmin=2).T
preprocessedFeatures = ["A: Skewness", "A: Skewtest", "B: Skewness", "B: Skewtest", "A: Skewtest pvalue",
'A: Kurtosis', 'A: Kurtosistest', 'B: normaltest pvalue', 'B: Skewtest',
'B: Skewness', 'A: normaltest pvalue', 'A: Kurtosistest pvalue', "A: Autocorrelation",
"A: Mean autocorrelation", "B: Autocorrelation", "B: Mean autocorrelation",
'B: Kurtosistest pvalue', 'B: Kurtosistest', 'B: Kurtosis', 'B: Skewtest pvalue',
'Kendall Tau', 'Linear regression pvalue', 'Ks 2samp', "VLTVar", "VLTMax", "VLTMean",
"VLTBinary", "VLTVarReversed", "VLTMaxReversed","VLTMeanReversed", "VLTBinaryReversed",
"Pwling1", "Pwling2", "Pwling3", "Pwling4", "Pwling5", "A: Normalized Entropy",
"B: Normalized Entropy", "Entropy Difference" ]
features = [['Number of Samples', 'A', SimpleTransform(transformer=len)],
['Type of Samples A', 'A type', CountVectorizer()],
['Type of Samples B', 'B type', CountVectorizer()],
['A: Skewness', 'A', SimpleTransform(transformer=skew)],
['A: Skewtest', 'A', SimpleTransform(transformer=ff.skewness)],
['A: Skewtest pvalue', 'A', SimpleTransform(transformer=ff.skewnessp)],
['A: Kurtosis', 'A', SimpleTransform(transformer=ff.kurt)],
['A: Kurtosistest', 'A', SimpleTransform(transformer=ff.kurtosist)],
['A: Kurtosistest pvalue', 'A', SimpleTransform(transformer=ff.kurtosistp)],
['A: normaltest pvalue', 'A', SimpleTransform(transformer=ff.nt)],
['A: Mean autocorrelation', 'A', SimpleTransform(transformer=ff.mean_autocorr)],
['A: Number of Unique Samples', 'A', SimpleTransform(transformer=ff.count_unique)],
['B: Number of Unique Samples', 'B', SimpleTransform(transformer=ff.count_unique)],
['B: Skewness', 'B', SimpleTransform(transformer=ff.skew)],
['B: Skewtest', 'B', SimpleTransform(transformer=ff.skewness)],
['B: normaltest pvalue', 'B', SimpleTransform(transformer=ff.nt)],
['Ks 2samp', ['A','B'], MultiColumnTransform(ff.ks)],
['Linear regression pvalue', ['A','B'], MultiColumnTransform(ff.lr)],
['Kendall Tau', ['A','B'], MultiColumnTransform(ff.kdt)],
['B: Skewtest pvalue', 'B', SimpleTransform(transformer=ff.skewnessp)],
['B: Kurtosis', 'B', SimpleTransform(transformer=ff.kurt)],
['B: Kurtosistest', 'B', SimpleTransform(transformer=ff.kurtosist)],
['B: Kurtosistest pvalue', 'B', SimpleTransform(transformer=ff.kurtosistp)],
['VLTVar', ['A','B'], MultiColumnTransform(ff.vertical_line_test_var)],
['VLTMax', ['A','B'], MultiColumnTransform(ff.vertical_line_test_max)],
['VLTMean', ['A','B'], MultiColumnTransform(ff.vertical_line_test_mean)],
['VLTVarReversed', ['A','B'], MultiColumnTransform(ff.vertical_line_test_var_rev)],
['VLTMaxReversed', ['A','B'], MultiColumnTransform(ff.vertical_line_test_max_rev)],
['VLTMeanReversed', ['A','B'], MultiColumnTransform(ff.vertical_line_test_mean_rev)],
['A: Normalized Entropy', 'A', SimpleTransform(transformer=ff.normalized_entropy)],
['B: Normalized Entropy', 'B', SimpleTransform(transformer=ff.normalized_entropy)],
['Pwling1', ['A','B'], MultiColumnTransform(ff.pwling1)],
['Pwling2', ['A','B'], MultiColumnTransform(ff.pwling2)],
['Pwling3', ['A','B'], MultiColumnTransform(ff.pwling3)],
['Pwling4', ['A','B'], MultiColumnTransform(ff.pwling4)],
['Pwling5', ['A','B'], MultiColumnTransform(ff.pwling5)],
['Pearson R', ['A','B'], MultiColumnTransform(ff.correlation)],
['Pearson R Magnitude', ['A','B'], MultiColumnTransform(ff.correlation_magnitude)],
['Entropy Difference', ['A','B'], MultiColumnTransform(ff.entropy_difference)]]