在真实数据集上进行离群点检测#

此示例说明了在真实数据集上进行鲁棒协方差估计的必要性。它对于离群点检测和更好地理解数据结构都很有用。

我们从 Wine 数据集中选择了两组各两个变量,以说明可以使用几种离群点检测工具进行何种类型的分析。出于可视化目的,我们正在使用二维示例,但应该意识到在高维中情况并非如此简单,正如将指出的那样。

在下面的两个示例中,主要结果是非鲁棒的经验协方差估计受到观测值异构结构的严重影响。尽管鲁棒协方差估计能够集中于数据分布的主要模式,但它仍然坚持数据应呈高斯分布的假设,这导致了对数据结构的估计存在偏差,但在某种程度上仍然准确。One-Class SVM 不假设数据分布的任何参数形式,因此可以更好地对数据的复杂形状进行建模。

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

第一个示例#

第一个示例说明了当存在离群点时,最小协方差行列式鲁棒估计器如何帮助集中在相关集群上。在这里,经验协方差估计被主集群之外的点扭曲了。当然,一些筛选工具会指出存在两个集群(支持向量机、高斯混合模型、单变量离群点检测等)。但如果这是一个高维示例,这些工具都不能如此容易地应用。

from sklearn.covariance import EllipticEnvelope
from sklearn.inspection import DecisionBoundaryDisplay
from sklearn.svm import OneClassSVM

estimators = {
    "Empirical Covariance": EllipticEnvelope(support_fraction=1.0, contamination=0.25),
    "Robust Covariance (Minimum Covariance Determinant)": EllipticEnvelope(
        contamination=0.25
    ),
    "OCSVM": OneClassSVM(nu=0.25, gamma=0.35),
}
import matplotlib.lines as mlines
import matplotlib.pyplot as plt

from sklearn.datasets import load_wine

X = load_wine()["data"][:, [1, 2]]  # two clusters

fig, ax = plt.subplots()
colors = ["tab:blue", "tab:orange", "tab:red"]
# Learn a frontier for outlier detection with several classifiers
legend_lines = []
for color, (name, estimator) in zip(colors, estimators.items()):
    estimator.fit(X)
    DecisionBoundaryDisplay.from_estimator(
        estimator,
        X,
        response_method="decision_function",
        plot_method="contour",
        levels=[0],
        colors=color,
        ax=ax,
    )
    legend_lines.append(mlines.Line2D([], [], color=color, label=name))


ax.scatter(X[:, 0], X[:, 1], color="black")
bbox_args = dict(boxstyle="round", fc="0.8")
arrow_args = dict(arrowstyle="->")
ax.annotate(
    "outlying points",
    xy=(4, 2),
    xycoords="data",
    textcoords="data",
    xytext=(3, 1.25),
    bbox=bbox_args,
    arrowprops=arrow_args,
)
ax.legend(handles=legend_lines, loc="upper center")
_ = ax.set(
    xlabel="ash",
    ylabel="malic_acid",
    title="Outlier detection on a real data set (wine recognition)",
)
Outlier detection on a real data set (wine recognition)

第二个示例#

第二个示例展示了最小协方差行列式鲁棒协方差估计器集中于数据分布主要模式的能力:位置似乎估计得很好,尽管由于香蕉形分布,协方差很难估计。无论如何,我们可以摆脱一些离群观测值。One-Class SVM 能够捕获真实的数据结构,但困难在于调整其核带宽参数,以便在数据散布矩阵的形状和数据过拟合风险之间取得良好的平衡。

X = load_wine()["data"][:, [6, 9]]  # "banana"-shaped

fig, ax = plt.subplots()
colors = ["tab:blue", "tab:orange", "tab:red"]
# Learn a frontier for outlier detection with several classifiers
legend_lines = []
for color, (name, estimator) in zip(colors, estimators.items()):
    estimator.fit(X)
    DecisionBoundaryDisplay.from_estimator(
        estimator,
        X,
        response_method="decision_function",
        plot_method="contour",
        levels=[0],
        colors=color,
        ax=ax,
    )
    legend_lines.append(mlines.Line2D([], [], color=color, label=name))


ax.scatter(X[:, 0], X[:, 1], color="black")
ax.legend(handles=legend_lines, loc="upper center")
ax.set(
    xlabel="flavanoids",
    ylabel="color_intensity",
    title="Outlier detection on a real data set (wine recognition)",
)

plt.show()
Outlier detection on a real data set (wine recognition)

脚本总运行时间: (0 minutes 0.318 seconds)

相关示例

在玩具数据集上比较用于离群点检测的异常检测算法

在玩具数据集上比较用于离群点检测的异常检测算法

使用局部离群点因子(LOF)进行离群点检测

使用局部离群点因子(LOF)进行离群点检测

鲁棒 vs 经验协方差估计

鲁棒 vs 经验协方差估计

具有协方差椭球的线性和二次判别分析

具有协方差椭球的线性和二次判别分析

由 Sphinx-Gallery 生成的图库