-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmlUtil.py
More file actions
462 lines (370 loc) · 17.3 KB
/
mlUtil.py
File metadata and controls
462 lines (370 loc) · 17.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# -*- coding: utf-8 -*-
""" Install these if needed"
#!pip3 install pydotplus
#!pip3 install mlxtend
"""### Load requisite libraries"""
# Commented out IPython magic to ensure Python compatibility.
import matplotlib.pylab as plt
import numpy as np
import pandas as pd
import itertools
import matplotlib.gridspec as gridspec
# %matplotlib inline
# %reload_ext autoreload
# %autoreload 2
#from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn import svm
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn import metrics
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, plot_confusion_matrix, plot_roc_curve
from mlxtend.data import iris_data
from sklearn.utils import resample
import seaborn as sns
from sklearn import datasets
import os
import math
from math import floor, ceil
"""Stuff for plotting trees"""
from six import StringIO
#from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
import pydotplus
"""plot data"""
def plotDT(dtree,featureNames = None,classNames=None):
dot_data = StringIO()
export_graphviz(dtree,
out_file=dot_data,
feature_names = featureNames,
class_names = classNames,
filled=True,
rounded=True,
special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
l=Image(graph.create_png())
return l
def TestAccuracy(clf,X_test,y_test, display=False):
y_pred = clf.predict( X_test.values)
results = ( y_test.values[:,0]==y_pred )
if display:
nEntries, dummy = X_test.shape
for i in range(nEntries):
print(X_test.iloc[i])
print("Class=%s Prediction %s %s\n"%(y_test.values[i,0],y_pred[i],results[i]) )
#print("prediction",results)
# accuracy
nTrue = np.sum( results )
nTot = np.float( np.shape( results)[0] )
accuracy = nTrue/nTot
print("Overall accuracy ",accuracy)
def TestAccuracySingle(clf,X_test,Y_test,idx=0):
print(X_test.iloc[idx])
result = clf.predict( [X_test.values[idx,:]])
print("prediction",result)
#print(Y_test.iloc[idx])
#TestAccuracy(clf,X_test,Y_test,display=True)
def DataCuration(df,features):
#df['decimal_place_2'] = df['decimal_place_2'].round(2)
# old data curation routine
if 0:
df['HELIX'] = df['HELIX'].round(2)
df['TURNS'] = df['TURNS'].round(2)
df['COILS'] = df['COILS'].round(2)
df['THREE-TEN'] = df['THREE-TEN'].round(2)
df['BETA'] = df['BETA'].round(2)
df['HBONDS'] = df['HBONDS'].round()
df['WATERS'] = df['WATERS'].round()
df['RMSD'] = df['RMSD'].round(2)
df['SASA'] = df['SASA'].round()
df['FOLDX'] = df['FOLDX'].round(2)
df['CONSERVATION'] = df['CONSERVATION'].round(2)
df['HYDROPHOBICITY'] = df['HYDROPHOBICITY'].round(2)
# new
skipList=['HBONDS','WATERS','SASA']
for feature in features:
if feature not in skipList:
df[feature]=df[feature].round(2)
else:
df[feature]=df[feature].round()
"""### Example with PAS data """
import pandas as pd
def run():
dataFile = "feature_sets/features-latest-sets1n2.txt"
df = pd.read_csv(dataFile, sep="\s+", comment='#')
features=["WATERS", "HBONDS", "RMSD", "SASA", "HELIX", "TURNS", "COILS", "THREE-TEN", "BETA"]
output = ["TRAFFICKING"]
MLClassifier(df, features, output)
"""
Initializes, trains and tests ML classifiers
Written for decision tree currently
Classifiers: DT, RF, SVM
"""
def MLClassifier(df,features,output,
classifier="DT",
display=False,random_state=50,predict=False,dfPred=None):
# number of features
nFeatures=len(features)
# number of features to consider when looking for the best split
maxFeatures=4
# rounding for some reason
DataCuration(df,features)
# get featrures/output
X = df[features]
Y = df[output]
# training
X_train, X_test, Y_train, Y_test =\
train_test_split(X, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#initializing the classifier and fitting it
### Decision Tree ###
if classifier=="DT":
print("Decision tree classifier")
# for single feaure
if nFeatures == 1:
clf = DecisionTreeClassifier(criterion = "entropy",random_state=50,max_depth=None, min_samples_leaf=1)
elif nFeatures < maxFeatures and nFeatures > 1:
clf = DecisionTreeClassifier(
criterion = "entropy",
#class_weight="balanced",
random_state = random_state, #set random number seed
max_depth=None,
max_features=nFeatures,# was 3,
min_samples_leaf=1) #min of samples needed at a node for it to split further
else:
clf = DecisionTreeClassifier(
criterion = "entropy",
#class_weight="balanced",
random_state = random_state, #set random number seed
max_depth=None,
max_features=maxFeatures,# was 3,
min_samples_leaf=1) #min of samples needed at a node for it to split further
model=clf.fit(X_train, Y_train)
### Random Forest ###
elif classifier=="RF":
# for single feaure
if nFeatures == 1:
clf = RandomForestClassifier(bootstrap=True, n_estimators=10000, criterion="entropy", \
max_depth=None, max_leaf_nodes=None, \
min_samples_leaf=1, min_samples_split=2, random_state=random_state)
elif nFeatures < maxFeatures and nFeatures > 1:
clf = RandomForestClassifier(bootstrap=True, n_estimators=10000, criterion="entropy", \
max_depth=None, max_features=nFeatures, max_leaf_nodes=None, \
min_samples_leaf=1, min_samples_split=2, random_state=random_state)
else:
clf = RandomForestClassifier(bootstrap=True, n_estimators=10000, criterion="entropy", \
max_depth=None, max_features=maxFeatures, max_leaf_nodes=None, \
min_samples_leaf=1, min_samples_split=2, random_state=random_state)
model=clf.fit(X_train, Y_train.values.ravel()) #Y_train)
# k-fold crossvalidation
kfold = KFold(n_splits=10, random_state=None)
results = cross_val_score(model, X, Y.values.ravel(), cv=kfold)
print('k-fold crossvalidation results')
print(results, '\n')
print('results mean')
print(results.mean(), '\n')
### SVM ###
elif classifier=="SVM":
#print("VERY BUGGY/INSUFFICIENT IMPLEMENTATION")
#clf = svm.SVC()
#clf.fit(X_train, np.ravel( Y_train) )
# using linear kernel since it is easy to plot feature importance, for others hard to interpret
clf = SVC(gamma='auto', random_state=random_state, max_iter=10000, kernel='linear', probability=True)
model=clf.fit(X_train, Y_train.values.ravel())
else:
raise RuntimeError(classifier+" not supported")
# print training accuracy
print("Training sample accuracy")
TestAccuracy(clf,X_train,Y_train)
#TestAccuracy(clf,X_test,Y_test, display=True)
print('Test Accuracy')
TestAccuracy(clf,X_test,Y_test)
### Print overall classification metrics
y_predict = clf.predict(X_test.values)
classNames=np.array(['non-trafficking', 'trafficking']) # need to verify, but I think this is correct
print(classification_report(Y_test, y_predict, target_names=classNames))
report=classification_report(Y_test, y_predict, target_names=classNames,output_dict=True)
f1s={'non-trafficking': report['non-trafficking']['f1-score'], 'trafficking': report['trafficking']['f1-score']}
f1ScoreMacro =metrics.f1_score(Y_test, y_predict, average='macro')
#if classifier=="SVM":
# print("BOWING OUT UNTIL I CAN RESOLVE SOME BUGS WITH SVMS")
# return None
### ROC
y_predict = clf.predict_proba(X_test.values)
fpr, tpr, thr = metrics.roc_curve(Y_test, y_predict[:,1], drop_intermediate=False)
auc = metrics.auc(fpr, tpr)
# predict on column E data
if predict:
DataCuration(dfPred,features)
X_pred=dfPred[features]
y_predict = clf.predict(X_pred)
dataframe=pd.DataFrame(y_predict)
dataframe.columns =['Prediction']
frames = [dfPred[['VARIANT']], dataframe]
result = pd.concat(frames, axis=1)
print(result)
if display:
plot = sns.pairplot(dfPred[features])
if display:
if classifier == "DT":
plotDT(clf,featureNames=features, classNames=classNames)
plt.savefig('dt_tree_md.pdf')
plt.figure()
plot_confusion_matrix(clf, X_test, Y_test)
plt.title("Confusion matrix")
plt.savefig(classifier+'_cm.png')
plt.figure()
plot_roc_curve(clf, X_test, Y_test)
plt.title("ROC curve")
plt.savefig(classifier+'_roc_features.pdf')
# print feature importance
plt.figure()
if classifier == "SVM":
#this option to plot importance features only apply to linear kernel in SVM
feat_importances = pd.Series(abs(clf.coef_[0]), index=X.columns)
else:
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(15).plot(kind='barh', color="red").grid(False)
#feat_importances.nlargest(10).plot(kind='barh', color="green")
plt.title('Feature Importances for '+classifier)
plt.tight_layout()
plt.savefig(classifier+'_fi.png')
# package
outputs = dict()
#outputs['cutoffs']=cutoffs
outputs['tprs']=tpr
outputs['fprs']=fpr
#outputs['tnrs']=tnrs
#outputs['fnrs']=fnrs
outputs['auc']=auc
outputs['f1score']=f1ScoreMacro
outputs['f1_classes']=f1s
return outputs
def OLD():
"""### generate data for ROC curve"""
HEX = df[['HELIX']]
B = df[["BETA"]]
T = df[["TURNS"]]
C = df[["COILS"]]
TT = df[["THREE-TEN"]]
R = df[["RMSD"]]
W = df[["WATERS"]]
H = df[["HBONDS"]]
S = df[["SASA"]]
Y = df["TRAFFICKING"]
#X = df[features]
#extracting metrics for whole model
y_predict = clf.fit(X_train, Y_train).predict_proba(X_test)
#y_predict = clf.fit(X_train, Y_train).decisions(X_test)
fpr, tpr, thr = metrics.roc_curve(Y_test, y_predict[:,1], drop_intermediate=False)
auc = metrics.auc(fpr, tpr)
#redifined the classifier for one feature
clf2_dt = DecisionTreeClassifier(criterion = "entropy",random_state=random_state,max_depth=None, min_samples_leaf=1)
#redifined the classifier for one feature
clf2_dt = DecisionTreeClassifier(criterion = "entropy",random_state=random_state,max_depth=None, min_samples_leaf=1)
#helicity
#split
HEX_train, HEX_test, HEY_train, HEY_test = train_test_split(HEX, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#train
#modelHE=clf2_dt.fit(HEX_train, HEY_train.values.ravel())
#train
#HEY_predict = clf2_dt.predict(HEX_test.values)
HEY_predict = clf2_dt.fit(HEX_train, HEY_train.values.ravel()).predict_proba(HEX_test.values)
f_helix, t_helix, th_helix = metrics.roc_curve(HEY_test, HEY_predict[:,1], pos_label=1, drop_intermediate=False)
helix_auc = metrics.auc(f_helix, t_helix)
#beta
bx_train, bx_test, by_train, by_test = train_test_split(B, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelB=clf2_dt.fit(bx_train, by_train.values.ravel())
#by_predict = clf2_dt.predict(bx_test.values)
by_predict = clf2_dt.fit(bx_train, by_train).predict_proba(bx_test)
f_beta, t_beta, th_beta = metrics.roc_curve(by_test, by_predict[:,1], pos_label=1, drop_intermediate=False)
beta_auc = metrics.auc(f_beta, t_beta)
#coil
cx_train, cx_test, cy_train, cy_test = train_test_split(C, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelC=clf2_dt.fit(cx_train, cy_train.values.ravel())
#cy_predict = clf2_dt.predict(cx_test.values)
cy_predict = clf2_dt.fit(cx_train, cy_train).predict_proba(cx_test)
f_coil, t_coil, th_coil = metrics.roc_curve(cy_test, cy_predict[:,1], pos_label=1, drop_intermediate=False)
coil_auc = metrics.auc(f_coil, t_coil)
#3-10
ttx_train, ttx_test, tty_train, tty_test = train_test_split(TT, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelTT=clf2_dt.fit(ttx_train, tty_train.values.ravel())
#tty_predict = clf2_dt.predict(ttx_test.values)
tty_predict = clf2_dt.fit(ttx_train, tty_train).predict_proba(ttx_test)
f_tten, t_tten, th_tten = metrics.roc_curve(tty_test, tty_predict[:,1], pos_label=1, drop_intermediate=False)
tten_auc = metrics.auc(f_tten, t_tten)
#rmsd
#extract metrics for rmsd. f_rmsd = false positive rate, t_rmsd=true positive rate, th_rmsd=threshold values
rx_train, rx_test, ry_train, ry_test = train_test_split(R, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelR=clf2_dt.fit(rx_train, ry_train.values.ravel())
#ry_predict = clf2_dt.predict(rx_test.values)
ry_predict = clf2_dt.fit(rx_train, ry_train).predict_proba(rx_test)
f_rmsd, t_rmsd, th_rmsd = metrics.roc_curve(ry_test, ry_predict[:,1], pos_label=1, drop_intermediate=False)
rmsd_auc = metrics.auc(f_rmsd, t_rmsd)
#extract metrics for sasa
sx_train, sx_test, sy_train, sy_test = train_test_split(S, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelS=clf2_dt.fit(sx_train, sy_train.values.ravel())
#sy_predict = clf2_dt.predict(sx_test.values)
sy_predict = clf2_dt.fit(sx_train, sy_train).predict_proba(sx_test)
f_sasa, t_sasa, th_sasa = metrics.roc_curve(sy_test, sy_predict[:,1], pos_label=1, drop_intermediate=False)
sasa_auc = metrics.auc(f_sasa, t_sasa)
#hbonds
#extract metrics for hbonds
hx_train, hx_test, hy_train, hy_test = train_test_split(H, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelH=clf2_dt.fit(hx_train, hy_train.values.ravel())
#hy_predict = clf2_dt.predict(hx_test.values)
hy_predict = clf2_dt.fit(hx_train, hy_train).predict_proba(hx_test)
f_hbonds, t_hbonds, th_hbonds = metrics.roc_curve(hy_test, hy_predict[:,1], pos_label=1, drop_intermediate=False)
hbonds_auc = metrics.auc(f_hbonds, t_hbonds)
#water
#extract metrics for water
wx_train, wx_test, wy_train, wy_test = train_test_split(W, Y, train_size=0.7, test_size=0.3, random_state=random_state)
#modelW=clf2_dt.fit(wx_train, wy_train.values.ravel())
#wy_predict = clf2_dt.predict(wx_test.values)
wy_predict = clf2_dt.fit(wx_train, wy_train).predict_proba(wx_test)
f_water, t_water, th_water = metrics.roc_curve(wy_test, wy_predict[:,1], pos_label=1)
water_auc = metrics.auc(f_water, t_water)
#turns
tx_train, tx_test, ty_train, ty_test = train_test_split(T, Y, train_size=0.7, test_size=0.3, random_state=random_state)
modelT=clf2_dt.fit(tx_train, ty_train.values.ravel())
#ty_predict = clf2_dt.predict(tx_test.values)
ty_predict = clf2_dt.fit(tx_train, ty_train).predict_proba(tx_test)
f_turns, t_turns, th_turns = metrics.roc_curve(ty_test, ty_predict[:,1], pos_label=1, drop_intermediate=False)
turns_auc = metrics.auc(f_turns, t_turns)
"""### Plot ROC"""
#plot the roc curves of rmsd, waters and hbonds. auc is area under the curve
#plot_roc_curve(clf, X_test, Y_test)
plt.figure()
plt.plot (fpr, tpr, color='cyan', label="DT Classifier(AUC=%0.2f)"% auc)
if False:
plt.plot (f_tten, t_tten, label="3-10 (AUC=%0.2f)"% tten_auc)
plt.plot(f_helix, t_helix, label="HELIX(AUC=%0.2f)"% helix_auc)
plt.plot (f_beta, t_beta, label="BETA (AUC=%0.2f)"% beta_auc)
plt.plot (f_sasa, t_sasa, label="SASA (AUC=%0.2f)"% sasa_auc)
plt.plot (f_water, t_water, label="Waters (AUC=%0.2f)"% water_auc)
plt.plot (f_coil, t_coil, label="Coils (AUC=%0.2f)"% coil_auc)
plt.plot (f_turns, t_turns, label="TURNS (AUC=%0.2f)"% turns_auc)
plt.plot(f_rmsd, t_rmsd, label="RMSD(AUC=%0.2f)"% rmsd_auc)
plt.plot (f_hbonds, t_hbonds, label="H-Bonds (AUC=%0.2f)"% hbonds_auc)
plt.plot([0, 1], [0, 1], color='navy', linestyle='--', label='Random')
plt.title('ROC curve-MD features (DT)')
plt.xlabel("FPR")
plt.ylabel("TPR")
#plt.legend(loc="upper left")
plt.legend(bbox_to_anchor=(1,0.9), loc='upper left')
plt.savefig('dt_roc_md.pdf', bbox_inches='tight')
outputs = dict()
#outputs['cutoffs']=cutoffs
outputs['tprs']=tpr
outputs['fprs']=fpr
#outputs['tnrs']=tnrs
#outputs['fnrs']=fnrs
outputs['auc']=auc
return outputs
#run()