forked from MRN-Code/polyssifier
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolyssifier.py
More file actions
320 lines (267 loc) · 11.3 KB
/
polyssifier.py
File metadata and controls
320 lines (267 loc) · 11.3 KB
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
#import matplotlib
#matplotlib.use('Agg')
import sys
sys.setrecursionlimit(10000)
import argparse
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
# from sklearn.lda import LDA
import numpy as np
import multiprocessing
import logging
from sklearn.cross_validation import StratifiedKFold
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import f1_score, confusion_matrix
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import VotingClassifier
from sklearn.externals import joblib
#from mlp import MLP
import time
import os
import pandas as pd
import matplotlib.pyplot as plt
import pickle as p
logging.basicConfig(format="[%(module)s:%(levelname)s]:%(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
PROCESSORS = int(multiprocessing.cpu_count() * 3 / 4)
def make_voter(estimators, y, voting='hard'):
estimators = list(estimators.items())
clf = VotingClassifier(estimators, voting)
clf.estimators_ = [estim for name, estim in estimators]
clf.le_ = LabelEncoder()
clf.le_.fit(y)
clf.classes_ = clf.le_.classes_
return clf
def make_argument_parser():
'''
Creates an ArgumentParser to read the options for this script from
sys.argv
'''
parser = argparse.ArgumentParser()
parser.add_argument('data_directory',
help='Directory where the data files live.')
parser.add_argument('data', default='data.npy',
help='Data file name')
parser.add_argument('label', default='labels.npy',
help='label file name')
parser.add_argument('--level', default='info',
help='Logging level')
return parser
class Poly:
def __init__(self, data, label, n_folds=10,
scale=True, verbose=10, exclude=[],
feature_selection=False):
logger.info('Building classifiers ...')
self.classifiers = {
#'Multilayer Perceptron': {
# 'clf': MLP(verbose=0, patience=500, learning_rate=1,
# n_hidden=10, n_deep=2, l1_norm=0,
# drop=0),
# 'parameters': {}},
'Nearest Neighbors': {
'clf': KNeighborsClassifier(3),
'parameters': {'n_neighbors': [1, 5, 10, 20]}},
'SVM': {
'clf': SVC(C=1, probability=True,
cache_size=10000),
'parameters': {'kernel': ['linear', 'rbf', 'poly'],
'C': [0.01, 0.1, 1]}},
'Decision Tree': {
'clf': DecisionTreeClassifier(max_depth=None,
max_features='auto'),
'parameters': {}},
'Random Forest': {
'clf': RandomForestClassifier(max_depth=None,
n_estimators=10,
max_features='auto'),
'parameters': {'n_estimators': list(range(5, 20))}},
'Logistic Regression': {
'clf': LogisticRegression(fit_intercept=False,
solver='lbfgs', penalty='l2'),
'parameters': {'C': [0.001, 0.1, 1]}},
'Naive Bayes': {
'clf': GaussianNB(),
'parameters': {}},
}
logger.info('Done.')
# Remove classifiers that want to be excluded
for key in exclude:
if key in self.classifiers:
del self.classifiers[key]
self.exclude = exclude
self.feature_selection = feature_selection
self.n_folds = n_folds
self.scale = scale
self._le = LabelEncoder()
self.label = self._le.fit_transform(label)
self.n_class = len(np.unique(label))
self.verbose = verbose
self.data = data
self.scores = {}
self.confusions = {}
self._predictions = {}
self._test_index = []
self.predictions = None
zeros = np.zeros((self.n_class, self.n_class))
for key in self.classifiers:
self.scores[key] = {'train': [], 'test': []}
self.confusions[key] = np.copy(zeros)
self._predictions[key] = []
if 'Voting' not in self.exclude:
self.scores['Voting'] = {'train': [], 'test': []}
self.confusions['Voting'] = np.copy(zeros)
self._predictions['Voting'] = []
def fit(self, X, y, n=0):
# Fits data on all classifiers
# Checks if data was already fitted
if self.n_class == 2:
average = 'binary'
else:
average = 'weighted'
self.fitted_clfs = {}
for key, val in self.classifiers.items():
file_name = 'models/{}_{}.p'.format(key, n+1)
start = time.time()
if os.path.isfile(file_name):
logger.info('Loading {}'.format(file_name))
clf = joblib.load(file_name)
else:
logger.info('Running {}'.format(key))
if val['parameters']:
if key == 'Multilayer Perceptron':
njobs = 1
else:
njobs = PROCESSORS
clf = GridSearchCV(val['clf'], val['parameters'],
n_jobs=njobs, cv=3, iid=False)
else:
clf = val['clf']
clf.fit(X, y)
joblib.dump(clf, file_name)
duration = time.time()-start
ypred = clf.predict(X)
score = f1_score(y, ypred, average=average)
self.scores[key]['train'].append(score)
self.fitted_clfs[key] = clf
logger.info('{0:25}: Train {1:.2f}, {2:.2f} sec'.format(
key, score, duration))
# build the voting classifier
if 'Voting' not in self.exclude:
logger.info('Running Voting Classifier')
clf = make_voter(self.fitted_clfs, y, 'hard')
self.fitted_clfs['Voting'] = clf
ypred = clf.predict(X)
score = f1_score(y, ypred, average=average)
self.scores['Voting']['train'].append(score)
logger.info('{0:25} : Train {1:.2f}'.format('Voting', score))
def test(self, X, y):
if self.n_class == 2:
average = 'binary'
else:
average = 'weighted'
for key, val in self.fitted_clfs.items():
ypred = val.predict(X)
# Scores
score = f1_score(y, ypred, average=average)
self.scores[key]['test'].append(score)
# Confusion matrix
confusion = confusion_matrix(y, ypred)
self.confusions[key]+=confusion
# Predictions
self._predictions[key].extend(
self._le.inverse_transform(ypred))
logger.info('{0:25} : Test {1:.2f}'.format(key, score))
def run(self):
if not os.path.exists('models'):
os.makedirs('models')
if self.scale:
sc = StandardScaler()
self.data = sc.fit_transform(self.data)
if self.feature_selection:
anova_filter = SelectKBest(f_regression, k='all')
temp = int(np.round(self.data.shape[1]/5))
name = lambda x:\
x['clf']._final_estimator.__class__.__name__.lower()
for key, val in self.classifiers.items():
self.classifiers[key]['clf'] = make_pipeline(
anova_filter, self.classifiers[key]['clf'])
new_dict = {}
for keyp in self.classifiers[key]['parameters']:
new_dict[name(self.classifiers[key])+'__'+keyp]\
= self.classifiers[key]['parameters'][keyp]
self.classifiers[key]['parameters'] = new_dict
self.classifiers[key]['parameters']['selectkbest__k']\
= np.arange(temp, self.data.shape[1]-temp, temp).tolist()
kf = StratifiedKFold(self.label, n_folds=self.n_folds,
random_state=1988)
for n, (train, test) in enumerate(kf):
logger.info('Fold {}'.format(n+1))
X_train, y_train = self.data[train, :], self.label[train]
X_test, y_test = self.data[test, :], self.label[test]
self.fit(X_train, y_train, n)
self.test(X_test, y_test)
self._test_index.extend(test)
self.predictions = pd.DataFrame(self._predictions)
self.predictions['index'] = self._test_index
self.predictions.set_index('index', inplace=True)
self.predictions.sort_index(inplace=True)
return self.scores
def plot(self, file_name='temp'):
df = pd.DataFrame(
[(key, np.mean(score['train']), np.std(score['train']),
np.mean(score['test']), np.std(score['test']))
for key, score in self.scores.items()],
columns=['classifier', 'Train score',
'Train std', 'Test score',
'Test std'])
df.sort_values('Test score', ascending=False, inplace=True)
df = df.set_index('classifier')
print(df)
error = df[['Train std', 'Test std']]
error.columns = ['Train score', 'Test score']
data = df[['Train score', 'Test score']]
nc = df.shape[0]
ax1 = data.plot(kind='bar', yerr=error, colormap='Blues',
figsize=(nc*2, 5), alpha=0.7)
ax1.set_xticklabels([])
ax1.set_xlabel('')
ax1.yaxis.grid(True)
ylim = np.max(np.array(data).min()-.1, 0)
ax1.set_ylim(ylim, 1)
for n, rect in enumerate(ax1.patches):
if n >= nc:
break
ax1.text(rect.get_x()-rect.get_width()/2., ylim + (1-ylim)*.01,
data.index[n], ha='center', va='bottom',
rotation='90', color='black', fontsize=15)
ax1.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15),
ncol=2, fancybox=True, shadow=True)
plt.savefig(file_name + '.pdf')
plt.savefig(file_name + '.svg', transparent=False,
bbox_inches='tight', pad_inches=0)
# saving confusion matrices
with open('confusions.pkl', 'wb') as f:
p.dump(self.confusions, f, protocol=2)
return (ax1, df)
if __name__ == '__main__':
parser = make_argument_parser()
args = parser.parse_args()
if args.level == 'info':
logger.setLevel(logging.INFO)
else:
logger.setLevel(logging.DEBUG)
data = np.load(args.data_directory + args.data)
label = np.load(args.data_directory + args.label)
logger.info(
'Starting classification with {} workers'.format(PROCESSORS))
poly = Poly(data, label, n_folds=5)
poly.run()
poly.plot(args.data_directory + args.data)