-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrain_ReducedFeatures.py
346 lines (279 loc) · 12.2 KB
/
Train_ReducedFeatures.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
from random import *
def readDATA(N):
import pandas as pd
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
import seaborn as sns
##read the data
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data",
names = ["class","cap-shape","cap-surface","cap-color","bruises","odor","gill-attachment","gill-spacing",
"gill-size","gill-color","stalk-shape","stalk-root","stalk-surface-above-ring",
"stalk-surface-below-ring","stalk-color-above-ring","stalk-color-below-ring","veil-type",
"veil-color","ring-number","ring-type","spore-print-color","population","habitat"],
header = None)
#df.describe()
##remove the feature with missing values
df = df.drop('stalk-root',1)
feature_columns = df.columns[1:]
for i, f in zip(np.arange(1, len(feature_columns) + 1), feature_columns):
print('feature {:d}:\t{}'.format(i, f))
#Check chi2 significance of 22 features
from sklearn.feature_selection import chi2, SelectKBest
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
numeric_data = pd.DataFrame()
for f in feature_columns:
numeric_data[f] = le.fit_transform(df[f])
chi_statics, p_values = chi2(numeric_data, df['class'])
chi2_result = pd.DataFrame({'features': feature_columns, 'chi2_statics': chi_statics})
chi2_result.dropna(axis=0, how='any', inplace=True)
print(chi2_result.sort_values(by='chi2_statics', ascending=False)[['features', 'chi2_statics']].reset_index().drop('index', axis=1))
%matplotlib inline
_ = chi2_result.sort_values(by='chi2_statics', ascending=True).set_index('features')['chi2_statics'].plot(kind='barh', logx=True, rot=-2)
#choose most descriptive features for learning
top_features = chi2_result.sort_values(by='chi2_statics', ascending=False)['features'].head(N).values
print('top ' ,N, ' most useful features are:')
for i in top_features:
print(i)
feature_important = pd.DataFrame()
for j in top_features:
dumm = df[j].str.get_dummies()
dumm.columns = ['{}_{}'.format(j, v) for v in dumm.columns]
feature_important = pd.concat([feature_important, dumm], axis=1)
feature_important['class'] = df['class']
row, column = feature_important.shape
#1-hot encoding features
enc = LabelEncoder()
enc.fit(feature_important[feature_important.columns[column-1]])
feature_important[feature_important.columns[column-1]] = enc.transform(feature_important[feature_important.columns[column-1]])
#train and test split.
train, test = train_test_split(feature_important,train_size = 0.8)
X_train = train.drop('class',1)
X_test = test.drop('class',1)
y_train = train['class']
y_test = test['class']
return X_train, X_test, y_train, y_test
def LogReg(X_train, y_train, X_test, y_test):
#Logistic Regression
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(X_train, y_train)
a=clf.score(X_train, y_train)
b=clf.score(X_test, y_test)
print("Default Logistic Regression: training accuracy: ",a," testing accuracy: ",b)
#a roc plot
y_prob = clf.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities
y_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.
clf.score(X_test, y_pred)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
#confusion matrix
from sklearn.metrics import confusion_matrix
p = clf.predict(X_test)
cm = confusion_matrix(y_test,p)
print(cm)
tn, fp, fn, tp = cm.ravel()
Precision = (tp/(tp+fp))
Recall = (tp/(tp+fn))
print("tn: ",tn,"fp: ",fp,"fn:",fn,"tp: ",tp)
print("Precision: ",Precision, "Recall: ", Recall, "F1: ", ((2*Precision*Recall)/(Precision+Recall)))
return a,b
def KNN(X_train, y_train, X_test, y_test):
#KNN
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit (X_train, y_train)
p = knn.predict(X_test)
a = knn.score(X_train, y_train)
b = knn.score(X_test, y_test)
print("KNN: training accuracy: ",a," testing accuracy: ",b)
#roc plot
y_prob = knn.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities
y_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.
knn.score(X_test, y_pred)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
#confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,p)
print(cm)
tn, fp, fn, tp = cm.ravel()
Precision = (tp/(tp+fp))
Recall = (tp/(tp+fn))
print("tn: ",tn,"fp: ",fp,"fn:",fn,"tp: ",tp)
print("Precision: ",Precision, "Recall: ", Recall, "F1: ", ((2*Precision*Recall)/(Precision+Recall)))
return a,b
def NN(X_train, y_train, X_test, y_test):
# Neural Network
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes = (30, 30), activation='logistic', alpha=0.001, solver='lbfgs', learning_rate='constant')
mlp.fit(X_train, y_train)
p = mlp.predict(X_test)
a = mlp.score(X_train, y_train)
b = mlp.score(X_test, y_test)
print("Default NN: training accuracy: ",a," testing accuracy: ",b)
#roc plot
y_prob = mlp.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities
y_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.
mlp.score(X_test, y_pred)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,p)
print(cm)
tn, fp, fn, tp = cm.ravel()
Precision = (tp/(tp+fp))
Recall = (tp/(tp+fn))
print("tn: ",tn,"fp: ",fp,"fn:",fn,"tp: ",tp)
print("Precision: ",Precision, "Recall :", Recall, "F1: ", ((2*Precision*Recall)/(Precision+Recall)))
return a,b
def Tree(X_train, y_train, X_test, y_test):
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier()
tree.fit(X_train, y_train)
p = tree.predict(X_test)
a = tree.score(X_train, y_train)
b = tree.score(X_test, y_test)
print("Default Decision Tree: training accuracy: ",a," testing accuracy: ",b)
#roc curve
y_prob = tree.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities
y_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.
tree.score(X_test, y_pred)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
from sklearn.metrics import confusion_matrix
#confusion matrix
cm = confusion_matrix(y_test,p)
print(cm)
tn, fp, fn, tp = cm.ravel()
Precision = (tp/(tp+fp))
Recall = (tp/(tp+fn))
print("tn: ",tn,"fp: ",fp,"fn:",fn,"tp: ",tp)
print("Precision: ",Precision, "Recall :", Recall, "F1: ", ((2*Precision*Recall)/(Precision+Recall)))
return a,b
def NB(X_train, y_train, X_test, y_test):
from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(X_train, y_train)
p = nb.predict(X_test)
a = nb.score(X_train, y_train)
b = nb.score(X_test, y_test)
print("Deafult Naive Bayes: training accuracy: ",a," testing accuracy: ",b)
#roc curve
y_prob = nb.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities
y_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.
nb.score(X_test, y_pred)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.title('Receiver Operating Characteristic')
plt.plot(false_positive_rate,true_positive_rate, color='red',label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],linestyle='--')
plt.axis('tight')
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
#confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test,p)
print(cm)
tn, fp, fn, tp = cm.ravel()
Precision = (tp/(tp+fp))
Recall = (tp/(tp+fn))
print("tn: ",tn,"fp: ",fp,"fn:",fn,"tp: ",tp)
print("Precision: ",Precision, "Recall :", Recall, "F1: ", ((2*Precision*Recall)/(Precision+Recall)))
return a,b
N = 1
Accu_Train_Reg = np.zeros(10)
Accu_Test_Reg = np.zeros(10)
Accu_Train_KNN = np.zeros(10)
Accu_Test_KNN = np.zeros(10)
Accu_Train_NN = np.zeros(10)
Accu_Test_NN = np.zeros(10)
Accu_Train_Tree = np.zeros(10)
Accu_Test_Tree = np.zeros(10)
Accu_Train_NB = np.zeros(10)
Accu_Test_NB = np.zeros(10)
while (N <= 10):
X_train, X_test, y_train, y_test = readDATA(N) #given the number of feature will be used in training
#logistic regression
a,b = LogReg(X_train, y_train, X_test, y_test)
Accu_Train_Reg[N-1] = a
Accu_Test_Reg[N-1] = b
#KNN
c,d = KNN(X_train, y_train, X_test, y_test)
Accu_Train_KNN[N-1] = c
Accu_Test_KNN[N-1] = d
#Neural Network
e,f = NN(X_train, y_train, X_test, y_test)
Accu_Train_NN[N-1] = e
Accu_Test_NN[N-1] = f
#Decision Tree
g,h = Tree(X_train, y_train, X_test, y_test)
Accu_Train_Tree[N-1] = g
Accu_Test_Tree[N-1] = h
#Naive Bayes
i,l = NB(X_train, y_train, X_test, y_test)
Accu_Train_NB[N-1] = i
Accu_Test_NB[N-1] = l
N += 1
print (Accu_Train_Reg)
print (Accu_Test_Reg)
print (Accu_Train_KNN)
print (Accu_Test_KNN)
print (Accu_Train_NN)
print (Accu_Test_NN)
print (Accu_Train_Tree)
print (Accu_Test_Tree)
print (Accu_Train_NB)
print (Accu_Test_NB)