-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml16_ex.py
64 lines (58 loc) · 1.8 KB
/
ml16_ex.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
#For digits dataset in sklearn.dataset, please try following
#classifiers and find out the one that gives best performance. Also find the optimal parameters for that classifier.
from sklearn import datasets
digits=datasets.load_digits()
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier
model_params = {
'svm': {
'model': svm.SVC(gamma='auto'),
'params': {
'C': [1, 10, 20],
'kernel': ['rbf', 'linear']
}
},
'random_forest': {
'model': RandomForestClassifier(),
'params': {
'n_estimators': [1, 5, 10]
}
},
'logistic_regression': {
'model': LogisticRegression(solver='liblinear', multi_class='auto'),
'params': {
'C': [1, 5, 10]
}
},
'naive_bayes_gaussian': {
'model': GaussianNB(),
'params': {}
},
'naive_bayes_multinomial': {
'model': MultinomialNB(),
'params': {}
},
'decision_tree': {
'model': DecisionTreeClassifier(),
'params': {
'criterion': ['gini', 'entropy'],
}
}
}
from sklearn.model_selection import GridSearchCV
import pandas as pd
scores=[]
for model_name,mp in model_params.items():
clf=GridSearchCV(mp['model'],mp['params'],cv=5,return_train_score=False)
clf.fit(digits.data,digits.target)
scores.append({
'model':model_name,
'best_score':clf.best_score_,
'best_params':clf.best_params_
})
df=pd.DataFrame(scores,columns=['model','best_score','best_params'])
print(df)