SVM用sklearn庫實現(一)
阿新 • • 發佈:2019-01-05
** SVM support_vector_machines 支援向量機
1、解決的是二分類問題
2、什麼樣的決策邊界才是最好的。
3、找到一條線(面),使離該線最近的點,能夠最遠。最近的點稱為支援向量,該線是分類線。
code
- 訓練一個基本的SVM,線性的
from sklearn.svm import SVC # "Support vector classifier"
model = SVC(kernel='linear')
model.fit(X, y)```
得到的結果為:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0 ,
decision_function_shape=None, degree=3, gamma='auto', kernel='linear',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
可以從中看出那些重要的引數如 C , gamma, kernel等
2. 定義繪圖函式
def plot_svc_decision_function(model, ax=None, plot_support=True): if ax is None: ax = plt.gca()#Get Current Axes獲取當前軸線 xlim = ax.get_xlim() ylim = ax.get_ylim() # create grid to evaluate model x = np.linspace(xlim[0], xlim[1], 30) y = np.linspace(ylim[0], ylim[1], 30) Y, X = np.meshgrid(y, x) xy = np.vstack([X.ravel(), Y.ravel()]).T P = model.decision_function(xy).reshape(X.shape) # plot decision boundary and margins ax.contour(X, Y, P, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--']) # plot support vectors if plot_support: ax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], s=300, linewidth=1, facecolors='none'); ax.set_xlim(xlim) ax.set_ylim(ylim)```
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
plot_svc_decision_function(model);
即可畫出圖一。
3.檢視支援向量:
model.support_vectors_
array([[ 0.44359863, 3.11530945],
[ 2.33812285, 3.43116792],
[ 2.06156753, 1.96918596]])
4.引入核函式:
#加入徑向基函式, 核設為 rbf
clf = SVC(kernel='rbf', C=1E6 )
clf.fit(X, y)
5.調節SVM引數: Soft Margin問題:
調節C引數
當C趨近於無窮大時:意味著分類嚴格不能有錯誤
當C趨近於很小的時:意味著可以有更大的錯誤容忍
X, y = make_blobs(n_samples=100, centers=2,
random_state=0, cluster_std=0.8)
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
for axi, C in zip(ax, [10.0, 0.1]):
model = SVC(kernel='linear', C=C).fit(X, y)
axi.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
plot_svc_decision_function(model, axi)
axi.scatter(model.support_vectors_[:, 0],
model.support_vectors_[:, 1],
s=300, lw=1, facecolors='none');
axi.set_title('C = {0:.1f}'.format(C), size=14)
6.調節gamma值,gamma值越大,對映的維度越高,模型越複雜:
X, y = make_blobs(n_samples=100, centers=2,
random_state=0, cluster_std=1.1)
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
# wspace調整影象邊框,使得各個圖之間的間距為0
for axi, gamma in zip(ax, [10.0, 0.1]):
model = SVC(kernel='rbf', gamma=gamma).fit(X, y)
axi.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
plot_svc_decision_function(model, axi)
axi.scatter(model.support_vectors_[:, 0],
model.support_vectors_[:, 1],
s=300, lw=1, facecolors='none');
axi.set_title('gamma = {0:.1f}'.format(gamma), size=14)
ps:排版不好,請勿見怪。裡面的內容較為粗淺。希望能一起交流。