【问题标题】:How to plot ROC curve for Local Outlier Factor algorithm?如何绘制局部异常因子算法的 ROC 曲线?
【发布时间】:2018-07-30 13:18:58
【问题描述】:

我正在尝试使用Local Outlier Factor (LOF) 算法,并想绘制 ROC 曲线。问题是,scikit-learn 提供的库不会为每个预测生成 score

那么,有没有办法解决这个问题?

【问题讨论】:

  • ROC 曲线仅用于概率预测。然而,上述算法会产生二进制标签(0/1),所以这不起作用

标签: python scikit-learn roc


【解决方案1】:

属性negative_outlier_factor_ 实际上是documentation 中所述的-LOF,在source code 中更好。一种常见的方法是通过根据阈值的不同值分配您的预测来计算 ROC。如果您的数据位于df 中且标签位于'label' 列中,则代码将如下所示:

def get_predictions(lof, threshold=1.5):
    return list(map(lambda x: -1 if x > threshold else 1, lof))

lof_ths = np.arange(1.3, 6., 0.1)
clf = LocalOutlierFactor(n_neighbors=30)
clf.fit_predict(df.drop(['label'], axis=1))
lofs = -clf.negative_outlier_factor_

plt.figure()
lw = 2
plt.xlim([0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate', fontsize=14)
plt.ylabel('True Positive Rate', fontsize=14)
plt.title('Receiver operating characteristic', fontsize=16)

fpr = tpr = [0]

for ths in lof_ths:
    pred = get_predictions(lof, threshold=ths)
    tn, fp, fn, tp = confusion_matrix(df.label, pred, labels=[-1,1]).ravel()
    fpr.append(fp / (fp + tn + .000001))
    tpr.append(tp / (tp + fn + .000001))

fpr.append(1)
tpr.append(1)
plt.plot(fpr, tpr, label='ROC Curve')
plt.plot([0, 1], [0, 1], color='navy', label='Random Guessing', lw=lw, linestyle='--')
plt.legend(loc="lower right", fontsize=12)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 2016-02-04
    • 2019-04-11
    • 1970-01-01
    • 2019-02-27
    • 2017-09-13
    • 2021-03-03
    • 2014-09-20
    相关资源
    最近更新 更多