開始使用免費開始

移除樣本的影響

支援向量是指會影響決策邊界的訓練樣本。在這個練習中,你將把非支援向量從訓練集移除,來觀察這種行為。

紅酒品質資料集已經載入到 Xy(僅使用前 2 個特徵)。(注意:我們在 plot_classifier() 中指定了 lims,讓兩張圖使用相同的座標範圍,方便直接比較。)

本練習屬於課程

Python 中的線性分類器

檢視課程

練習說明

  • 在整個資料集上訓練一個線性 SVM。
  • 建立一個只包含支援向量的新資料集。
  • 在這個較小的資料集上再訓練一個線性 SVM。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Train a linear SVM
svm = SVC(kernel="linear")
svm.fit(____)
plot_classifier(X, y, svm, lims=(11,15,0,6))

# Make a new data set keeping only the support vectors
print("Number of original examples", len(X))
print("Number of support vectors", len(svm.support_))
X_small = X[____]
y_small = y[____]

# Train a new SVM using only the support vectors
svm_small = SVC(kernel="linear")
svm_small.fit(____)
plot_classifier(X_small, y_small, svm_small, lims=(11,15,0,6))
編輯並執行程式碼