開始使用免費開始

再談 contamination

你會注意到 one-class SVM 沒有 contamination 參數。不過你現在很清楚,需要有方式來控制被標記為新奇點的樣本比例,才能控制你的偽陽性率。所以你決定實驗以門檻方式處理分數。已經匯入的偵測器是 onesvm,你也有可用的資料 X_trainX_testy_trainy_test,以及 numpy 作為 np,還有 confusion_matrix()

本練習屬於課程

在 Python 設計機器學習工作流程

檢視課程

練習說明

  • 訓練 1-class SVM,並對測試資料計算分數。
  • 計算測試資料中觀察到的離群值比例。
  • 使用 np.quantile() 找出應該在哪裡設定分數門檻,才能達到該比例。
  • 依該門檻為測試資料貼標。列印混淆矩陣。

動手互動練習

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

# Fit a one-class SVM detector and score the test data
nov_det = ____(X_train)
scores = ____(X_test)

# Find the observed proportion of outliers in the test data
prop = np.____(y_test==____)

# Compute the appropriate threshold
threshold = np.____(____, ____)

# Print the confusion matrix for the thresholded scores
print(confusion_matrix(y_test, ____ > ____))
編輯並執行程式碼