再次讨论污染率
您会注意到,单类 SVM 没有 contamination 参数。但您已经很清楚,需要一种方式来控制被标记为新奇点的样本比例,从而控制您的误报率。因此,您决定对分数进行阈值化实验。检测器已作为 onesvm 导入,您还可以使用数据 X_train、X_test、y_train、y_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, ____ > ____))