Python-支援向量機SVM

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
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
#圖形中文顯示處理
matplotlib.rc('font', **{'sans-serif' : 'SimHei',
'family' : 'sans-serif'})
def plot_estimator(estimator, X, y, title):
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 ##加1減1只是為了繪圖時留空白
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1)) #meshgrid根據坐標向量創建坐標矩陣
Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()]) #一維,共3476筆資料
#ravel() Return a contiguous flattened array
#np.c_ 串接兩個list,np.ravel將矩陣變為一維
Z = Z.reshape(xx.shape) #二維,共44筆資料(44,79)
plt.plot()
#Contours(輪廓) can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity.
plt.contourf(xx, yy, Z, alpha=0.4, cmap = plt.cm.RdYlBu) #cmap- Colormap #alpha透明度,愈小愈透明0~1
plt.scatter(X[:, 0], X[:, 1], c=y, cmap = plt.cm.brg) #c - color
plt.title(title)
plt.xlabel('Petal.Length')
plt.ylabel('Petal.Width')
plt.show()
iris = load_iris()
X = iris.data[:,[2,3]]
y = iris.target[:]
#決策樹DecisionTree
clf = tree.DecisionTreeClassifier()
clf.fit(X, y)
#支援向量機SVM
clf1 = SVC(kernel="linear")
clf1.fit(X, y)
#羅吉斯迴歸
clf2 = LogisticRegression()
clf2.fit(X, y)
#隨機森林
clf3 = RandomForestClassifier(n_estimators=100, criterion="entropy", random_state=0)
clf3.fit(X, y)
plot_estimator(clf, X, y, "決策樹")
plot_estimator(clf1, X, y, "支援向量機")
plot_estimator(clf2, X, y, "羅吉斯迴歸")
plot_estimator(clf3, X, y, "隨機森林")

決策樹模型
支援向量機模型
羅吉斯迴歸模型
隨機森林模型

The C parameter tells the SVM optimization how much you want to avoid misclassifying each training example. For large values of C, the optimization will choose a smaller-margin hyperplane if that hyperplane does a better job of getting all the training points classified correctly. Conversely, a very small value of C will cause the optimizer to look for a larger-margin separating hyperplane, even if that hyperplane misclassifies more points. For very tiny values of C, you should get misclassified examples, often even if your training data is linearly separable.