-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParamsTuning.py
155 lines (132 loc) · 5.22 KB
/
ParamsTuning.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
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import RFECV
from sklearn.linear_model import SGDClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import LinearSVC, SVC
import numpy as np
def mlp_param_selection(X, y, n_folds, metric):
# griglia degli iperparametri
parameters = [{
'hidden_layer_sizes': [(100, 50, 25), (100, 50), (100,)],
'activation': ['tanh', 'relu'],
'solver': ['sgd', 'adam'],
'learning_rate_init': [.1, .01, 10 ** -3, 10 ** -4],
'learning_rate': ['constant', 'adaptive'],
}]
clf = model_selection.GridSearchCV(MLPClassifier(max_iter=10000), param_grid=parameters,
scoring=metric,
cv=n_folds, refit=True,
n_jobs=-1)
clf.fit(X, y)
print("Best parameters:")
print()
print(clf.best_params_)
print()
print("Grid scores:")
print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.4f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print()
return clf.best_estimator_
def svm_param_selection(X, y, n_folds, metric):
# griglia degli iperparametri
parameters = [{"kernel": ['rbf'], 'C': [0.1, 1, 10, 25, 50, 75, 100],
"gamma": [10 ** -4, 10 ** -3, 10 ** -2, 10 ** -1, 10, 10 ** 2, 10 ** 3, 10 ** 4],
"decision_function_shape": ["ovo", "ovr"]
},
{"kernel": ['linear'], "C": [0.1, 1, 10], "decision_function_shape": ["ovo", "ovr"]}]
clf = model_selection.GridSearchCV(SVC(), param_grid=parameters, scoring=metric,
cv=n_folds, refit=True,
n_jobs=-1)
clf.fit(X, y)
print("Best parameters:")
print()
print(clf.best_params_)
print()
print("Grid scores:")
print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.4f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print()
return clf.best_estimator_
def random_forest_param_selection(X, y, n_folds, metric):
# griglia degli iperparametri
param_grid ={'criterion': ['entropy', 'gini'],
'max_depth': list(np.linspace(10, 150, 10, dtype = int)),
'max_features': ['auto', 'sqrt','log2', None],
'min_samples_leaf': [4, 6, 8, 12],
'min_samples_split': [5, 7, 10, 14],
'n_estimators': list(np.linspace(150, 300, 10, dtype = int))}
clf = model_selection.GridSearchCV(RandomForestClassifier(), param_grid=param_grid, scoring=metric,
cv=n_folds, refit=True,
n_jobs=-1)
clf.fit(X, y)
print("Best parameters:")
print()
print(clf.best_params_)
print()
print("Grid scores:")
print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.4f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print()
return clf
def sgd_param_selection(X, y, n_folds, metric):
param_grid = {
"loss": ["hinge", "log", "squared_hinge", "modified_huber"],
'max_iter': [1000],
'l1_ratio': [0.08, 0.09, 0.1, 0.12, 0.13, 0.14, 0.15, 0.2],
"penalty": ["l2", "l1", 'elasticnet'],
}
clf = model_selection.GridSearchCV(SGDClassifier(max_iter=6000), param_grid=param_grid, scoring=metric,
cv=n_folds, refit=True,
n_jobs=-1)
clf.fit(X, y)
print("Best parameters:")
print()
print(clf.best_params_)
print()
print("Grid scores:")
print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.4f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print()
return clf
def knn_param_selection(X, y, n_folds, metric):
# griglia degli iperparametri
param_grid = {
'n_neighbors': [3, 5, 7, 11],
'metric': ["minkowski", "euclidean", "chebyshev"],
"p": [3,4,5]
}
clf = model_selection.GridSearchCV(KNeighborsClassifier(),param_grid=param_grid, scoring=metric,
cv=n_folds, refit=True,
n_jobs=-1)
clf.fit(X, y)
print("Best parameters:")
print()
print(clf.best_params_)
print()
print("Grid scores:")
print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.4f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print()
return clf