-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyper_parameter_tuning_classification.py
374 lines (303 loc) · 12.4 KB
/
hyper_parameter_tuning_classification.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
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
# -*- coding: utf-8 -*-
"""Hyper_parameter_tuning_Classification.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16LlFZTViSMscShOLpiE1OYW25oF_CNZa
# Hyper-parameter Tunning of Machine Learning (ML) Models
### Code for Classification Problems
#### `Dataset Used:`
MNIST dataset
#### `Machine Learning Algorithm Used:`
* Random Forest (RF)
* Support Vector Machine (SVM)
* K-Nearest Neighbor (KNN)
* Artificial Neural Network (ANN)
#### `Hyper-parameter Tuning Algorithms Used:`
* Grid Search
* Random Search
* Bayesian Optimization with Gaussian Processes (BO-GP)
* Bayesian Optimization with Tree-structured Parzen Estimator (BO-TPE)
---
"""
# Commented out IPython magic to ensure Python compatibility.
# Importing required libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
import scipy.stats as stats
from sklearn import datasets
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
"""#### Loading MNIST Dataset
The Modified National Institute of Standards and Technology (MNIST) database is a large database of handwritten digits that is commonly used by the people who want to try learning techniques and pattern recognition methods on real-world data while spending minimal efforts on preprocessing and formatting. It has a training set of 60,000 examples, and a test set of 10,000 examples.It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image.
It has 1797 record and 64 columns.
For more details about the dataset click here: [Details-1](https://yann.lecun.com/exdb/mnist/), [Details-2](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html#sklearn.datasets.load_digits/)
"""
# Loading the dataset
X, y = datasets.load_digits(return_X_y=True)
datasets.load_digits()
"""### Baseline Machine Learning Models: Classifier with default Hyper-parameters
### `Random Forest`
"""
# Random Forest (RF) with 3-fold cross validation
RF_clf = RandomForestClassifier()
RF_clf.fit(X,y)
RF_scores = cross_val_score(RF_clf, X, y, cv = 3,scoring = 'accuracy')
print("Accuracy (RF): "+ str(RF_scores.mean()))
"""### `Support Vector Machine`"""
# Support Vector Machine (SVM)
SVM_clf = SVC(gamma='scale')
SVM_clf.fit(X,y)
SVM_scores = cross_val_score(SVM_clf, X, y, cv = 3,scoring = 'accuracy')
print("Accuracy (SVM): "+ str(SVM_scores.mean()))
"""### `K-Nearest Neighbor`"""
# K-Nearest Neighbor (KNN)
KNN_clf = KNeighborsClassifier()
KNN_clf.fit(X,y)
KNN_scores = cross_val_score(KNN_clf, X, y, cv = 3,scoring='accuracy')
print("Accuracy (KNN):"+ str(KNN_scores.mean()))
"""### `Artificial Neural Network`"""
# Artificial Neural Network (ANN)
from keras.models import Sequential, Model
from keras.layers import Dense, Input
from keras.wrappers.scikit_learn import KerasClassifier
from keras.callbacks import EarlyStopping
def ann_model(optimizer = 'sgd',neurons = 32,batch_size = 32,epochs = 50,activation = 'relu',patience = 5,loss = 'categorical_crossentropy'):
model = Sequential()
model.add(Dense(neurons, input_shape = (X.shape[1],), activation = activation))
model.add(Dense(neurons, activation = activation))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer = optimizer, loss = loss)
early_stopping = EarlyStopping(monitor = "loss", patience = patience)
history = model.fit(X, pd.get_dummies(y).values, batch_size = batch_size, epochs=epochs, callbacks = [early_stopping], verbose=0)
return model
ANN_clf = KerasClassifier(build_fn = ann_model, verbose = 0)
ANN_scores = cross_val_score(ANN_clf, X, y, cv = 3,scoring = 'accuracy')
print("Accuracy (ANN):"+ str(ANN_scores.mean()))
"""### Hyper-parameter Tuning Algorithms
### `1] Grid Search`
"""
from sklearn.model_selection import GridSearchCV
"""### `Random Forest`"""
# Random Forest (RF)
RF_params = {
'n_estimators': [10, 20, 30],
'max_depth': [15,20,25,30,50],
"criterion":['gini','entropy']
}
RF_clf = RandomForestClassifier(random_state = 1)
RF_grid = GridSearchCV(RF_clf, RF_params, cv = 3, scoring = 'accuracy')
RF_grid.fit(X, y)
print(RF_grid.best_params_)
print("Accuracy (RF): "+ str(RF_grid.best_score_))
"""### `Support Vector Machine`"""
# Support Vector Machine (SVM)
SVM_params = {
'C': [1, 10, 20, 50, 100],
"kernel":['linear','poly','rbf','sigmoid']
}
SVM_clf = SVC(gamma='scale')
SVM_grid = GridSearchCV(SVM_clf, SVM_params, cv = 3, scoring = 'accuracy')
SVM_grid.fit(X, y)
print(SVM_grid.best_params_)
print("Accuracy:"+ str(SVM_grid.best_score_))
"""### `K-Nearest Neighbor`"""
#K-Nearest Neighbor (KNN)
KNN_params = { 'n_neighbors': [2, 4, 6, 8] }
KNN_clf = KNeighborsClassifier()
KNN_grid = GridSearchCV(KNN_clf, KNN_params, cv = 3, scoring = 'accuracy')
KNN_grid.fit(X, y)
print(KNN_grid.best_params_)
print("Accuracy:"+ str(KNN_grid.best_score_))
"""### `Artificial Neural Network`"""
# Artificial Neural Network (ANN)
ANN_params = {
'optimizer': ['adam','sgd'],
'activation': ['relu','tanh'],
'batch_size': [16,32],
'neurons':[16,32],
'epochs':[30,50],
'patience':[3,5]
}
ANN_clf = KerasClassifier(build_fn = ann_model, verbose = 0)
ANN_grid = GridSearchCV(ANN_clf, ANN_params, cv = 3,scoring = 'accuracy')
ANN_grid.fit(X, y)
print(ANN_grid.best_params_)
print("Accuracy (ANN): "+ str(ANN_grid.best_score_))
"""### `2] Random Search`"""
from sklearn.model_selection import RandomizedSearchCV
from random import randrange as sp_randrange
from scipy.stats import randint as sp_randint
"""### `Random Forest`"""
# Random Forest (RF)
RF_params = {
'n_estimators': sp_randint(10,100),
'max_depth': sp_randint(5,50),
"criterion":['gini','entropy']
}
RF_clf = RandomForestClassifier(random_state = 1)
RF_Random = RandomizedSearchCV(RF_clf, param_distributions = RF_params, n_iter = 20,cv = 3,scoring = 'accuracy')
RF_Random.fit(X, y)
print(RF_Random.best_params_)
print("Accuracy (RF):"+ str(RF_Random.best_score_))
"""### `Support Vector Machine`"""
# Support Vector Machine(SVM)
SVM_params = {
'C': stats.uniform(1,50),
"kernel":['poly','rbf']
}
SVM_clf = SVC(gamma='scale')
SVM_Random = RandomizedSearchCV(SVM_clf, param_distributions = SVM_params, n_iter = 20,cv = 3,scoring = 'accuracy')
SVM_Random.fit(X, y)
print(SVM_Random.best_params_)
print("Accuracy (SVM): "+ str(SVM_Random.best_score_))
"""### `K-Nearest Neighbor`"""
# K-Nearest Neighbor (KNN)
KNN_params = {'n_neighbors': range(1,20)}
KNN_clf = KNeighborsClassifier()
KNN_Random = RandomizedSearchCV(KNN_clf, param_distributions = KNN_params,n_iter = 10,cv = 3,scoring = 'accuracy')
KNN_Random.fit(X, y)
print(KNN_Random.best_params_)
print("Accuracy (KNN): "+ str(KNN_Random.best_score_))
"""### `Artificial Neural Network`"""
# Artificial Neural Network (ANN)
ANN_params = {
'optimizer': ['adam','sgd'],
'activation': ['relu','tanh'],
'batch_size': [16,32],
'neurons':sp_randint(10,100),
'epochs':[30,50],
'patience':sp_randint(5,20)
}
ANN_clf = KerasClassifier(build_fn = ann_model, verbose = 0)
ANN_Random = RandomizedSearchCV(ANN_clf, param_distributions = ANN_params, n_iter = 10,cv = 3,scoring = 'accuracy')
ANN_Random.fit(X, y)
print(ANN_Random.best_params_)
print("Accuracy (ANN): "+ str(ANN_Random.best_score_))
"""### `3] Bayesian Optimization with Gaussian Process (BO-GP)`"""
from skopt import Optimizer
from skopt import BayesSearchCV
from skopt.space import Real, Categorical, Integer
"""### `Random Factor`"""
#Random Forest (RF)
RF_params = {
'n_estimators': Integer(10,100),
'max_depth': Integer(5,50),
"criterion":['gini','entropy']
}
RF_clf = RandomForestClassifier(random_state = 1)
RF_Bayes = BayesSearchCV(RF_clf, RF_params,cv = 3,n_iter = 20, n_jobs = -1,scoring = 'accuracy')
RF_Bayes.fit(X, y)
print(RF_Bayes.best_params_)
print("Accuracy (RF): "+ str(RF_Bayes.best_score_))
"""### `Support Vector Machine`"""
# Support Vector Machine (SVM)
SVM_params = {
'C': Real(1,50),
"kernel":['poly','rbf']
}
SVM_clf = SVC(gamma = 'scale')
SVM_Bayes = BayesSearchCV(SVM_clf, SVM_params,cv = 3,n_iter = 20, n_jobs = -1,scoring = 'accuracy')
SVM_Bayes.fit(X, y)
print(SVM_Bayes.best_params_)
print("Accuracy (SVM): "+ str(SVM_Bayes.best_score_))
"""### `K-Nearest Neighbor`"""
# K-Nearest Neighbor (KNN)
KNN_params = {'n_neighbors': Integer(1,20),}
KNN_clf = KNeighborsClassifier()
KNN_Bayes = BayesSearchCV(KNN_clf, KNN_params,cv = 3,n_iter = 10, n_jobs = -1,scoring = 'accuracy')
KNN_Bayes.fit(X, y)
print(KNN_Bayes.best_params_)
print("Accuracy (KNN): "+ str(KNN_Bayes.best_score_))
"""### `Artificial Neural Network`"""
# Artificial Neural Network (ANN)
ANN_params = {
'optimizer': ['adam','sgd'],
'activation': ['relu','tanh'],
'batch_size': [16,32],
'neurons':Integer(10,100),
'epochs':[30,50],
'patience':Integer(5,20)
}
ANN_clf = KerasClassifier(build_fn = ann_model, verbose = 0)
ANN_Bayes = BayesSearchCV(ANN_clf, ANN_params,cv = 3,n_iter = 10, scoring = 'accuracy')
ANN_Bayes.fit(X, y)
print(ANN_Bayes.best_params_)
print("Accuracy (ANN): "+ str(ANN_Bayes.best_score_))
"""### `4] Bayesian Optimization with Tree-structured Parzen Estimator (BO-TPE)`"""
from sklearn.model_selection import StratifiedKFold
from hyperopt import hp, fmin, tpe, STATUS_OK, Trials
"""### `Random Forest`"""
# Random Forest (RF)
def RF_fun(params):
params = {
'n_estimators': int(params['n_estimators']),
'max_features': int(params['max_features']),
"criterion":str(params['criterion'])
}
RF_clf = RandomForestClassifier(**params)
RF_score = cross_val_score(RF_clf, X, y, cv = StratifiedKFold(n_splits = 3),scoring = 'accuracy').mean()
return {'loss':-RF_score, 'status': STATUS_OK }
RF_space = {
'n_estimators': hp.quniform('n_estimators', 10, 100, 1),
"max_features":hp.quniform('max_features', 1, 32, 1),
"criterion":hp.choice('criterion',['gini','entropy'])
}
RF_best = fmin(fn = RF_fun, space = RF_space, algo = tpe.suggest, max_evals = 20)
print("Estimated optimum (RF): " +str(RF_best))
"""### `Support Vector Machine`"""
# Support Vector Machine (SVM)
def SVM_fun(params):
params = {
'C': abs(float(params['C'])),
"kernel":str(params['kernel'])
}
SVM_clf = SVC(gamma ='scale', **params)
SVM_score = cross_val_score(SVM_clf, X, y, cv = StratifiedKFold(n_splits = 3), scoring ='accuracy').mean()
return {'loss':-SVM_score, 'status': STATUS_OK }
SVM_space = {
'C': hp.normal('C', 0, 50),
"kernel":hp.choice('kernel',['poly','rbf'])
}
SVM_best = fmin(fn = SVM_fun, space = SVM_space, algo = tpe.suggest, max_evals = 20)
print("Estimated optimum (SVM): "+str(SVM_best))
"""### `K-Nearest Neighbor`"""
# K-Nearest Neighbor (KNN)
def KNN_fun(params):
params = {'n_neighbors': abs(int(params['n_neighbors'])) }
KNN_clf = KNeighborsClassifier(**params)
KNN_score = cross_val_score(KNN_clf, X, y, cv = StratifiedKFold(n_splits=3), scoring='accuracy').mean()
return {'loss':-KNN_score, 'status': STATUS_OK }
KNN_space = {'n_neighbors': hp.quniform('n_neighbors', 1, 20, 1)}
KNN_best = fmin(fn = KNN_fun, space = KNN_space, algo = tpe.suggest, max_evals = 10)
print("Estimated optimum (KNN): "+str(KNN_best))
"""### `Artificial Neural Network`"""
# Artificial Neural Network (ANN)
def ANN_fun(params):
params = {
"optimizer":str(params['optimizer']),
"activation":str(params['activation']),
'batch_size': abs(int(params['batch_size'])),
'neurons': abs(int(params['neurons'])),
'epochs': abs(int(params['epochs'])),
'patience': abs(int(params['patience']))
}
ANN_clf = KerasClassifier(build_fn = ann_model,**params, verbose = 0)
ANN_score = -np.mean(cross_val_score(ANN_clf, X, y, cv=3, scoring = "accuracy"))
return {'loss':ANN_score, 'status': STATUS_OK }
ANN_space = {
"optimizer":hp.choice('optimizer',['adam','rmsprop','sgd']),
"activation":hp.choice('activation',['relu','tanh']),
'batch_size': hp.quniform('batch_size', 16, 32, 16),
'neurons': hp.quniform('neurons', 10, 100, 10),
'epochs': hp.quniform('epochs', 30, 50, 10),
'patience': hp.quniform('patience', 5, 20, 5),
}
ANN_best = fmin(fn = ANN_fun, space = ANN_space, algo = tpe.suggest, max_evals = 10)
print("Estimated optimum (ANN): "+str(ANN_best))
"""---"""