二维数据的异常值检测
我们将对二维葡萄酒数据集进行异常值检测。我们将使用三种不同的分类器来检测异常值:经验协方差、稳健协方差和单类支持向量机(One-Class SVM)。然后我们将绘制结果。
## Learn a frontier for outlier detection with several classifiers
xx1, yy1 = np.meshgrid(np.linspace(0, 6, 500), np.linspace(1, 4.5, 500))
for i, (clf_name, clf) in enumerate(classifiers.items()):
plt.figure(1)
clf.fit(X1)
Z1 = clf.decision_function(np.c_[xx1.ravel(), yy1.ravel()])
Z1 = Z1.reshape(xx1.shape)
plt.contour(
xx1, yy1, Z1, levels=[0], linewidths=2, colors=colors[i]
)
## Plot the results (= shape of the data points cloud)
plt.figure(1) ## two clusters
plt.title("Outlier detection on a real data set (wine recognition)")
plt.scatter(X1[:, 0], X1[:, 1], color="black")
plt.xlim((xx1.min(), xx1.max()))
plt.ylim((yy1.min(), yy1.max()))
plt.ylabel("ash")
plt.xlabel("malic_acid")
plt.show()