1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Wed Aug 22 18:05:44 2018
4
+
5
+ @author: Administrator
6
+ """
7
+
8
+ from sklearn .model_selection import cross_val_score , train_test_split
9
+ from sklearn .datasets import load_iris
10
+ from sklearn .linear_model import LogisticRegression
11
+ from sklearn .model_selection import StratifiedKFold
12
+ from sklearn .model_selection import GridSearchCV
13
+
14
+ iris = load_iris ()
15
+ log_reg = LogisticRegression ()
16
+
17
+ score = cross_val_score (log_reg , iris .data , iris .target ,cv = 10 )
18
+ print ("cross-vali score is: {}" .format (score .mean ()))
19
+
20
+ import mglearn
21
+ #mglearn.plots.plot_stratified_cross_validation()
22
+
23
+ kfold = StratifiedKFold (n_splits = 5 , shuffle = True )
24
+ for train_index , test_index in kfold .split (iris .data , iris .target ):
25
+ print (train_index , test_index )
26
+
27
+ from sklearn .svm import SVC
28
+
29
+ def simple_grid (iris , kfold ):
30
+ X_train ,X_test , y_train , y_test = train_test_split (
31
+ iris .data , iris .target , test_size = 0.3 ,random_state = 0 )
32
+ best_score = 0
33
+ para_list = [0.001 , 0.01 , 0.1 , 1 , 10 ]
34
+ for gamma in para_list :
35
+ for C in para_list :
36
+ svm = SVC (gamma = gamma , C = C )
37
+ #svm.fit(X_train, y_train)
38
+ scores = cross_val_score (svm , iris .data , iris .target ,cv = kfold )
39
+ score = scores .mean ()
40
+
41
+ if score > best_score :
42
+ best_score = score
43
+ best_para = {'C' :C , 'gamma' :gamma }
44
+ print ("best score is {:.2f}" .format (best_score ))
45
+ print ("best parameters is {}" .format (best_para ))
46
+ score = cross_val_score (svm , iris .data , iris .target ,cv = kfold )
47
+
48
+ print ("CV-score is {}" .format (score .mean (0 )))
49
+ return best_para
50
+
51
+ para = simple_grid (iris , kfold )
52
+
53
+ para_grid = {"C" :[0.001 , 0.01 , 0.1 , 1 , 10 ],
54
+ 'gamma' :[0.001 , 0.01 , 0.1 , 1 , 10 ]}
55
+ grid_search = GridSearchCV (SVC (), para_grid , cv = kfold )
56
+ X_train ,X_test , y_train , y_test = train_test_split (
57
+ iris .data , iris .target , test_size = 0.3 ,random_state = 0 )
58
+
59
+ grid_search .fit (X_train , y_train )
60
+ print ("best grid score is {:.2f}" .format (grid_search .score (X_test ,
61
+ y_test )))
62
+
63
+ import pandas as pd
64
+ results = pd .DataFrame (grid_search .cv_results_ )
65
+ display (results .head ())
66
+
67
+ print (cross_val_score (GridSearchCV (SVC (), para_grid , cv = kfold ),
68
+ X_train ,y_train , cv = kfold ).mean ())
69
+ y_pred = grid_search .predict (X_test ,y_test )
70
+
71
+ from sklearn .metrics import classification_report
72
+ print (classification_report (y_test , y_pred ))
0 commit comments