1. 程式人生 > >Support Vector Machine 支援向量機散點分類

Support Vector Machine 支援向量機散點分類

示例網址:
https://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html#sphx-glr-auto-examples-svm-plot-separating-hyperplane-py
支援向量機Scikit-learn官方文件:
https://scikit-learn.org/stable/modules/svm.html#svm-classification
支援向量機散點分類

import numpy as np
import matplotlib.pyplot as plt
from sklearn import
svm from sklearn.datasets import make_blobs X, y = make_blobs(n_samples=200, centers=2, random_state=6) # fit the model, don't regularize for illustration purposes clf = svm.SVC(kernel='linear', C=1000) clf.fit(X, y) plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap='Paired') # plot the decision function ax =
plt.gca() xlim = ax.get_xlim() ylim = ax.get_ylim() # create grid to evaluate model xx = np.linspace(xlim[0], xlim[1], 30) yy = np.linspace(ylim[0], ylim[1], 30) YY, XX = np.meshgrid(yy, xx) xy = np.vstack([XX.ravel(), YY.ravel()]).T # xy = np.c_[XX.ravel(), YY.ravel()]相同 Z = clf.decision_function(xy)
.reshape(XX.shape) # plot decision boundary and margins ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,linestyles=['--', '-', '--']) # plot support vectors ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=50,linewidth=1, facecolors='red') plt.show()

參考網址:

matplotlib contourf 畫等高線圖:
https://blog.csdn.net/Mr_Cat123/article/details/80677525

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-10,10,0.01)
y = np.arange(-10,10,0.01)
#也可以用x = np.linspace(-10,10,100)表示從-10到10,分100份

#將原始資料變成網格資料形式
X,Y = np.meshgrid(x,y)
Z = X**2+Y**2

#設定開啟畫布大小,長10,寬6
plt.figure(figsize=(10,6))
#填充顏色,f即filled
plt.contourf(X,Y,Z)
#畫等高線
plt.contour(X,Y,Z)
plt.show()

等高線圖

numpy.meshgrid(*xi, **kwargs):

Return coordinate matrices from two or more coordinate vectors.
Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.
https://docs.scipy.org/doc/numpy-1.8.1/reference/generated/numpy.meshgrid.html#numpy.meshgrid

>>> x = np.linspace(0, 1, 3)
>>> y = np.linspace(0, 1, 2)
>>> xv, yv = meshgrid(x, y)
>>> xv
array([[ 0. ,  0.5,  1. ],
       [ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]])

繪製三維曲面圖:
使用meshgrid函式以及Axes3D [plot_surface]
https://blog.csdn.net/Scc_hy/article/details/81455795

import mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4, 4, 0.25)
y = np.arange(-4, 4, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

ax.plot_surface(x, y, z, rstride = 1,   # row 行步長
                 cstride = 2,           # colum 列步長
                 cmap='hot')     	   # 漸變顏色
ax.contourf(x, y, z, 
            zdir='z',  # 使用資料方向
            offset=-2, # 填充投影輪廓位置
            cmap='hot')
ax.set_zlim(-2, 2)
plt.show()

三維曲面圖

注意事項:

kernel : string, optional (default=’rbf’)
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used.

decision_function(X):
Distance of the samples X to the separating hyperplane.

plt.gcf()和plt.gca():
當前的圖表和子圖可以使用plt.gcf()和plt.gca()獲得,分別表示"Get Current Figure"和"Get Current Axes"。

軟化因子C:
我們迄今為止的討論集中在非常乾淨的資料集,其中存在完美的決策邊界。 但是如果你的資料有一定的重疊呢?為了處理這種情況,SVM 實現了軟化因子,即“軟化”邊距:也就是說,如果允許更好的匹配,它允許某些點進入邊距。 邊緣的硬度由調整引數控制,通常稱為C。 對於非常大的C,邊距是硬的,點不能進入。 對於較小的C,邊緣較軟,可以擴充套件幷包含一些點。

clf = svm.SVC(kernel='linear', C=0.1)