-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
260 lines (215 loc) · 8.65 KB
/
generate.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
"""
This code generates 41 structural features and forms classifier dataset
"""
import os
import pandas as pd
import numpy as np
from joblib import Parallel, delayed
from tqdm import tqdm
from scipy.stats import chisquare, entropy
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_validate
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier, AdaBoostClassifier, ExtraTreesClassifier, \
RandomForestClassifier
from sklearn import preprocessing
from sklearn.neural_network import MLPClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier, NearestCentroid
from sklearn.naive_bayes import GaussianNB, BernoulliNB, ComplementNB, MultinomialNB
from sklearn.linear_model import RidgeClassifier, SGDClassifier, LogisticRegression
def kl_divergence(p, q):
"""
Calculate KL divergence to two distributions
:param p: First distribution
:param q: Second distribution
:return: KL divergence value
"""
kl = np.sum(np.where(p != 0, p * np.log(p / q), 0))
if np.isnan(kl) | np.isinf(kl):
kl = 0
return kl
def rademacher(input_vector):
"""
Calculate Rademacher complexity
:param input_vector: The input vector to calculate Rademacher complexity against
:return: Rademacher complexity value
"""
m = input_vector.shape[0]
unq = input_vector.unique()
sigma = np.random.randint(low=unq.min(), high=unq.max() + 1, size=m)
rad = np.sum(np.multiply(np.equal(sigma, input_vector), 1) * (1 / len(unq))) / m
return rad
def normalize(x):
"""
Perform normalization on the input
:param x: Input dataset
:return: Normalized dataset
"""
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
normalized = ps.DataFrame(x_scaled)
return normalized
def calculate_features(data_path):
"""
Calculate 41 structural characteristics of each dataset
:param data_path: Input data path
:return: A row containing 41 structural characteristics
"""
out_row = []
# Reading data file
df = ps.read_csv(data_path, header=None)
# Normalizing dataset
df.iloc[:, :-1] = normalize(df.iloc[:, :-1].values)
# Remove zeroed columns
df = df.loc[:, (df != 0).any(axis=0)]
# Impute the dataset
imputer = SimpleImputer(missing_values=np.nan, strategy='mean').fit(df)
df = ps.DataFrame(imputer.transform(df))
# Add header to dataframe
cols = []
for h in range(df.shape[1]):
cols.append('f' + str(h))
df.columns = cols
# Fix index
data = df
data = data.reset_index(drop=True)
# Number of samples and features, and division
m = df.shape[0]
n = df.shape[1]
out_row.append(m)
out_row.append(n)
out_row.append(m / n)
# Number of unique classes
out_row.append(df.iloc[:, -1].nunique())
# Mean of all data
out_row.append(((df.iloc[:, :-1]).mean()).mean())
# STD of all data
out_row.append(((df.iloc[:, :-1]).std()).mean())
# Entropy of class
ent = entropy(df.iloc[:, -1])
if np.isfinite(ent):
out_row.append(entropy(df.iloc[:, -1]))
else:
out_row.append(0)
# Quantile (25% and 75%), and range
seventy_five = np.percentile(df.iloc[:, -1], 75, interpolation='higher')
twenty_five = np.percentile(df.iloc[:, -1], 25, interpolation='higher')
out_row.append(seventy_five)
out_row.append(twenty_five)
out_row.append(seventy_five - twenty_five)
# Chi-square of class
chi, p = chisquare(df)
out_row.append(chi[-1])
# Average of Chi-square of all
out_row.append(np.mean(chi[:-1]))
# Median of Chi-square of all
out_row.append(np.median(chi[:-1]))
# Standard deviation of Chi-square of all
out_row.append(np.std(chi[:-1]))
# Min of Chi-square of all
out_row.append(np.min(chi[:-1]))
# Max of Chi-square of all
out_row.append(np.max(chi[:-1]))
# Pearson correlation
corr = df.corr(method='pearson')
# corr 2 class
out_row.append((corr.iloc[-1, :]).mean())
# corr 2 all
out_row.append(((corr.iloc[:-1, :-1]).mean()).mean())
# Kendall correlation
corr = df.corr(method='kendall')
# corr 2 class
out_row.append((corr.iloc[-1, :]).mean())
# corr 2 all
out_row.append(((corr.iloc[:-1, :-1]).mean()).mean())
# Spearman correlation
corr = df.corr(method='spearman')
# corr 2 class
out_row.append((corr.iloc[-1, :]).mean())
# corr 2 all
out_row.append(((corr.iloc[:-1, :-1]).mean()).mean())
# Covariance
cov = df.cov()
# cov 2 class
out_row.append((cov.iloc[-1, :]).mean())
# cov 2 all
out_row.append(((cov.iloc[:-1, :-1]).mean()).mean())
# KL Divergence
nd = np.random.normal(loc=1, scale=2, size=m)
ud = np.random.uniform(size=m)
ld = np.random.logistic(loc=1, scale=2, size=m)
ed = np.random.exponential(scale=2, size=m)
cd = np.random.chisquare(df=2, size=m)
rd = np.random.rayleigh(scale=2, size=m)
pd = np.random.pareto(a=2, size=m)
zd = np.random.zipf(a=2, size=m)
distros = ['nd', 'ud', 'ld', 'ed', 'cd', 'rd', 'pd', 'zd']
for distro in distros:
val = eval(distro)
# KL 2 all
tmp = ps.DataFrame(df.iloc[:, :-1])
res = tmp.apply(lambda x: kl_divergence(x, val), axis=0)
out_row.append(res.mean())
# KL 2 class
res = kl_divergence(df.iloc[:, -1], val)
out_row.append(res)
# Rademacher's complexity of class
out_row.append(rademacher(df.iloc[:, -1]))
# Convert to dataframe
out_row = np.array(out_row)
out_row = ps.DataFrame(out_row.reshape(-1, len(out_row)))
return out_row, data
def calculate_accuracy(df, cls):
"""
Calculate classification accuracy using 10cv
:param df: Input dataframe
:param cls: Classifier's name
:return: Accuracy
"""
try:
cv = cross_validate(cls, df.iloc[:, :-1], df.iloc[:, -1], cv=10)
accuracy = np.mean(cv['test_score'])
return cls, accuracy
except:
return np.NAN
def main():
if not os.path.exists('results'):
os.mkdir('results')
results_path = os.path.join(os.getcwd(), 'results')
data_path = os.path.join(os.getcwd(), 'eeg')
classifiers = [AdaBoostClassifier(), BernoulliNB(), ComplementNB(),
DecisionTreeClassifier(criterion="entropy"),
DecisionTreeClassifier(), ExtraTreesClassifier(n_jobs=-1),
GaussianNB(), GradientBoostingClassifier(),
GradientBoostingClassifier(criterion="squared_error"),
KNeighborsClassifier(n_jobs=-1, algorithm="ball_tree"),
KNeighborsClassifier(n_jobs=-1, algorithm="brute"),
KNeighborsClassifier(n_jobs=-1, algorithm="kd_tree"),
LinearDiscriminantAnalysis(solver="lsqr"),
LinearDiscriminantAnalysis(),
LogisticRegression(n_jobs=-1), LogisticRegression(n_jobs=-1, penalty="none"),
MLPClassifier(max_iter=500), MultinomialNB(), NearestCentroid(),
RandomForestClassifier(n_jobs=-1), RidgeClassifier(), SGDClassifier(n_jobs=-1)]
features = ['m', 'n', 'm/n', 'nClass', 'meanAll', 'avgSTDAll', 'entropyClass', 'Q75', 'Q25', 'Q75-Q25',
'ChiClass', 'avgChiAll', 'medChiAll', 'stdChiAll', 'minChiAll', 'maxChiAll', 'avgPearCorrClass',
'avgPearCorrAll', 'avgKendCorrClass', 'avgKendCorrAll', 'avgSpeaCorrClass', 'avgSpeaCorrAll',
'avgCovClass', 'avgCovAll', 'avgKLNormAll', 'avgKLUnifAll', 'avgKLLogiAll', 'avgKLExpoAll',
'avgKLChiAll', 'avgKLRaylAll', 'avgKLParetAll', 'avgKLZipfAll', 'KLNormClass', 'KLUnifClass',
'KLLogiClass', 'KLExpoClass', 'KLChiClass', 'KLRaylClass', 'KLParetClass', 'KLZipfClass', 'radComClass']
out = ps.DataFrame(columns=features)
acc = ps.DataFrame()
for root, directories, files in os.walk(data_path, topdown=False):
for i, name in enumerate(tqdm(files)):
row, data = calculate_features(os.path.join(data_path, name))
row.columns = features
out = out.append(row, ignore_index=True)
result = Parallel(n_jobs=-1)(delayed(calculate_accuracy)(data, eval(cls)) for cls in classifiers)
for j in range(len(classifiers)):
cls = str(result[j][0]).replace("'", '')
acc.loc[i, cls] = result[j][1]
for k in range(len(classifiers)):
out['class'] = acc.loc[:, acc.columns[k]]
out.to_csv(os.path.join(results_path, acc.columns[k] + '.csv'), index=False)
if __name__ == "__main__":
main()