-
Notifications
You must be signed in to change notification settings - Fork 1
/
ml_methods.py
220 lines (172 loc) · 8.32 KB
/
ml_methods.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
# Importing some useful/necessary packages
import numpy as np
import pandas as pd
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import StratifiedShuffleSplit, cross_val_score, GridSearchCV
from sklearn.metrics import accuracy_score, log_loss
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
import helper as hpr
def run_naive_bayes(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
clf = GaussianNB().fit(X_train, y_train) # Instantiate a classifier and fit this classifier to the data
print ('ML Model: Naive Bayes')
# Cross-validation
scores = cross_val_score(GaussianNB(), train.values, labels, cv=ss_split)
print ('Mean Cross-validation scores: {}'.format(np.mean(scores)))
# Accuracy
train_predictions = clf.predict(X_test)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = clf.predict_proba(X_test)
ll = log_loss(y_test, train_predictions_p)
test_predictions = clf.predict_proba(test)
return test_predictions, acc, ll
def run_support_vector_machine(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
clf = SVC(probability=True)
# Gird search
#param_grid = {'C': [1, 10, 100, 1000, 10000, 100000],
# 'gamma': [1, 10, 100, 1000, 10000, 100000]}
param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100]}
grid_search = GridSearchCV(SVC(probability=True), param_grid=param_grid, cv=ss_split)
grid_search.fit(X_train, y_train)
print ('Best parameter: {}'.format(grid_search.best_params_))
print ('Best cross-validation accuracy score: {}'.format(grid_search.best_score_))
print ('\nBest estimator:\n{}'.format(grid_search.best_estimator_))
# results = pd.DataFrame(grid_search.cv_results_)
# Show the first 5 rows of the result
#print results.head()
# scores = np.array(results.mean_test_score).reshape(6, 6)
#
# ax = sns.heatmap(scores, annot=True, fmt=".2f",linewidths=.5);
# ax.invert_yaxis()
# ax.set(xticklabels=param_grid['gamma']); ax.set(yticklabels=param_grid['C'])
# plt.yticks(rotation=0)
# plt.xlabel('gamma'); plt.ylabel('C'); plt.show()
print ('ML Model: Suppoort Vector Machine')
# Accuracy
train_predictions = grid_search.predict(X_test)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = grid_search.predict_proba(X_test)
ll = log_loss(y_test, train_predictions_p)
test_predictions = grid_search.predict_proba(test)
return test_predictions, acc, ll
def run_logistic_regression(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
#param_grid = {'C':[1, 10],
# 'tol': [0.001, 0.0001]}
# Standardize the training data.
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
scaler = StandardScaler().fit(X_test)
X_test_scaled = scaler.transform(X_test)
param_grid = {'C': [ 1000, 10000],
'tol': [0.000001, 0.00001]}
log_reg = LogisticRegression(solver='newton-cg', multi_class='multinomial')
grid_search = GridSearchCV(log_reg, param_grid, scoring='neg_log_loss', refit='True', n_jobs=1, cv=ss_split)
grid_search.fit(X_train_scaled, y_train)
print ('Best parameter: {}'.format(grid_search.best_params_))
print ('Best cross-validation neg_log_loss score: {}'.format(grid_search.best_score_))
print ('\nBest estimator:\n{}'.format(grid_search.best_estimator_))
print ('ML Model: Logistic Regression')
# Accuracy
train_predictions = grid_search.predict(X_test_scaled)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = grid_search.predict_proba(X_test_scaled)
ll = log_loss(y_test, train_predictions_p)
scaler = StandardScaler().fit(test)
test_scaled = scaler.transform(test)
test_predictions = grid_search.predict_proba(test_scaled)
# visualize error
# hpr.visualize_error(train_predictions, y_test)
return test_predictions, acc, ll
def run_k_nearest_neighbours(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
clf = KNeighborsClassifier(3) # Instantiate a classifier
clf.fit(X_train, y_train) # Fit this classifier to the data
print ('ML Model: K-Nearest Neighbours')
# Cross-validation
scores = cross_val_score(KNeighborsClassifier(3), train.values, labels, cv=ss_split)
#print 'Mean Cross-validation scores: {}'.format(np.mean(scores))
# Accuracy
train_predictions = clf.predict(X_test)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = clf.predict_proba(X_test)
ll = log_loss(y_test, train_predictions_p)
test_predictions = clf.predict_proba(test)
return test_predictions, acc, ll
def run_linear_discriminant_analysis(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
clf = LinearDiscriminantAnalysis().fit(X_train, y_train)
print ('ML Model: Linear Discriminant Analysis')
train_predictions = clf.predict(X_test)
acc = accuracy_score(y_test, train_predictions)
train_predictions_p = clf.predict_proba(X_test)
ll = log_loss(y_test, train_predictions_p)
test_predictions = clf.predict_proba(test)
return test_predictions, acc, ll
def run_decision_tree(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
clf = DecisionTreeClassifier().fit(X_train, y_train)
print ('ML Model: Decision Tree')
# Accuracy
train_predictions = clf.predict(X_test)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = clf.predict_proba(X_test)
ll = log_loss(y_test, train_predictions_p)
test_predictions = clf.predict_proba(test)
return test_predictions, acc, ll
def run_random_forest(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
clf = RandomForestClassifier().fit(X_train, y_train)
print ('ML Model: Random Forest')
# Accuracy
train_predictions = clf.predict(X_test)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = clf.predict_proba(X_test)
ll = log_loss(y_test, train_predictions_p)
test_predictions = clf.predict_proba(test)
return test_predictions, acc, ll
def run_mlp_neural_network(train, test, ss_split, labels):
# prepare training and test data
X_train, X_test, y_train, y_test = hpr.prepData(train, test, ss_split, labels);
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
scaler = StandardScaler().fit(X_test)
X_test_scaled = scaler.transform(X_test)
print ('ML Model: MLP Neural Network')
model = MLPClassifier(hidden_layer_sizes=(150,),activation='logistic',solver='lbfgs',alpha=0.001
,max_iter=200,early_stopping=True,validation_fraction=0.2,
learning_rate='adaptive',tol=1e-8,random_state=1).fit(X_train_scaled,y_train)
# Accuracy
train_predictions = model.predict(X_test_scaled)
acc = accuracy_score(y_test, train_predictions)
# Logloss
train_predictions_p = model.predict_proba(X_test_scaled)
ll = log_loss(y_test, train_predictions_p)
scaler = StandardScaler().fit(test)
test_scaled = scaler.transform(test)
test_predictions = model.predict_proba(test_scaled)
return test_predictions, acc, ll