gpt4 book ai didi

python - 如何绘制局部离群因子算法的 ROC 曲线?

转载 作者:行者123 更新时间:2023-11-30 22:20:19 28 4
gpt4 key购买 nike

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

那么,有什么办法可以解决这个问题吗?

最佳答案

属性负异常因子_实际上是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)

关于python - 如何绘制局部离群因子算法的 ROC 曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48880849/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com