【发布时间】:2020-09-27 12:32:36
【问题描述】:
我有一个大的 csv 文件,其中包含 2 列代表 k-means 聚类的结果。我计算了 11 个质心,csv 文件包含哪个最接近以及该点与该质心的距离。
条目如下所示:
K11-closest,K11-distance
0,31544.821603570384
0,31494.23348984612
0,31766.471900874752
0,31710.896696452823
然后我想使用我在 scikit-learn.org 上找到的脚本计算和绘制 LOF
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import LocalOutlierFactor
dataset = pd.read_csv('0.csv')
clf = LocalOutlierFactor(n_neighbors=20)
# use fit_predict to compute the predicted labels of the training samples
# (when LOF is used for outlier detection, the estimator has no predict,
# decision_function and score_samples methods).
y_pred = clf.fit_predict(dataset)
X_scores = clf.negative_outlier_factor_
plt.title("Local Outlier Factor (LOF)")
plt.scatter(dataset.iloc[:, 0], dataset.iloc[:, 1], color='k', s=3., label='Data points')
# plot circles with radius proportional to the outlier scores
radius = (X_scores.max() - X_scores) / (X_scores.max() - X_scores.min())
plt.scatter(dataset.iloc[:, 0].values, dataset.iloc[:, 1].values, s=50 * radius, edgecolors='r',
facecolors='none', label='Outlier scores')
plt.show()
但情节显示: 黑点是日期点,红色是一个圆圈,显示它有多少是异常值
所以我假设不是为每个点计算 LOF。但为什么?以及我如何计算每一点?并使其在情节中可见
【问题讨论】:
标签: python-3.x machine-learning scikit-learn data-science