-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgridsearch.py
53 lines (43 loc) · 1.65 KB
/
gridsearch.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
from sklearn import svm
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.model_selection import GridSearchCV,train_test_split
from sklearn.ensemble import IsolationForest
from sklearn.metrics import f1_score, make_scorer
def GridSearch(X,Y,estimator_str):
ss = StandardScaler()
ss.fit(X)
X = ss.transform(X)
# Take PCA to reduce feature space dimensionality
pca = PCA(n_components=512, whiten=True)
pca = pca.fit(X)
X = pca.transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.5, random_state=0)
f1sc = make_scorer(f1_score(y_train, y_test, average='micro'))
if estimator_str == 'svc':
parameters = {'kernel':('linear', 'rbf'),'C':[10],'gamma': [1e-3, 1e-4]}
estimator = svm.SVC()
clf = GridSearchCV(estimator, parameters)
clf.fit(X, Y)
elif estimator_str == 'isolationforest':
estimator = IsolationForest()
estimator.contamination = sum(Y==-1)/len(Y)
parameters = {"n_estimators": (40, 55, 75, 95, 115)}
clf = GridSearchCV(estimator, param_grid=parameters,scoring=f1sc)
clf.fit(X_train, y_train)
print("Best parameters set found on train set:")
print()
print(clf.best_params_)
print()
print("Grid scores on train set:")
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.3f (+/-%0.03f) for %r"% (mean, std * 2, params))
print()
#Let's experiment on best_params
return(clf.best_params_)
#if __name__ == '__main__':
#GridSearch()