-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathsvm-rbf分類-03-鳶尾花-gridSearch.txt
113 lines (79 loc) · 2.57 KB
/
svm-rbf分類-03-鳶尾花-gridSearch.txt
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
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from time import time
# 訓練資料
# 依序為花萼長度(sepal_length), 花萼寬度(sepal_width), 花瓣長度(petal_length), 花瓣寬度(petal_width), 品種(species)
data=np.genfromtxt('iris.csv', dtype=None, delimiter=',')
X=[]
Y=[]
# 訓練資料範圍, 為了繪圖用
a_min=999
a_max=-999
b_min=999
b_max=-999
for (sepal_length, sepal_width, petal_length, petal_width, species) in data:
s=species.decode('UTF-8')
#a=sepal_length
#b=sepal_width
a=petal_length
b=petal_width
if a>a_max: a_max=a
if a<a_min: a_min=a
if b>b_max: b_max=b
if b<b_min: b_min=b
X.append([a, b])
# 品種共有: setosa, versicolor, virginica
if s=='virginica':
Y.append(0)
else:
Y.append(1)
X=np.array(X)
Y=np.array(Y)
#=================================
# 建立分類模型
#=================================
# 找出較好的 C 及 gamma 參數
t0 = time()
param_grid = {'C': [0.1, 0.5, 1, 5, 10, 15, 20, 100, 150, 175, 200, 250, 1000],
'gamma': [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2, 3, 4, 5, 10], }
clf = GridSearchCV(SVC(kernel='rbf'), param_grid)
clf = clf.fit(X, Y)
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_)
best_C=clf.best_estimator_.C
best_gamma=clf.best_estimator_.gamma
print('C=', best_C)
print('gamma=', best_gamma)
clf = svm.SVC(kernel='rbf', gamma=best_gamma, C=best_C)
clf.fit(X, Y)
# 設定圖形尺寸
plt.figure(figsize=(12, 9))
#plt.clf()
# 畫出 support vectors
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=30, facecolors='y', zorder=10)
plt.axis('tight')
# 由訓練資料的標籤畫出不同顏色區域
x_min = a_min - 1.5
x_max = a_max + 1.5
y_min = b_min - 1.5
y_max = b_max + 1.5
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
Z = Z.reshape(XX.shape)
plt.pcolormesh(XX, YY, Z > 0, cmap=plt.cm.Paired)
plt.contour(XX, YY, Z, colors=['k', 'k', 'k'], linestyles=['--', '-', '--'], levels=[-.5, 0, 0.5])
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
# 畫出 training vectors
label_0 = np.array(Y) == 0
X_0 = X[label_0]
label_1 = np.array(Y) == 1
X_1 = X[label_1]
plt.scatter(X_0[:,0], X_0[:,1], c='b', s=120)
plt.scatter(X_1[:,0], X_1[:,1], c='g', s=120)
# 繪圖
plt.show()