gpt4 book ai didi

python - 绘制最近质心的 ROC 曲线

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:53 24 4
gpt4 key购买 nike

我想绘制一条 ROC 曲线来评估经过训练的最近质心分类器。我的代码适用于朴素贝叶斯、SVM、kNN 和 DT,但每当我尝试绘制最近质心的曲线时,都会出现异常,因为估计器没有 .predict_proba() 方法:

AttributeError: 'NearestCentroid' object has no attribute 'predict_proba'

绘制曲线的代码是

def plot_roc(self):
plt.clf()

for label, estimator in self.roc_estimators.items():
estimator.fit(self.data_train, self.target_train)
proba_for_each_class = estimator.predict_proba(self.data_test)

fpr, tpr, thresholds = roc_curve(self.target_test, proba_for_each_class[:, 1])

plt.plot(fpr, tpr, label=label)

plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Luck', alpha=.8)

plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.legend()
plt.show()

self.roc_estimators 是一个字典,我在其中存储经过训练的估计器和分类器的标签,如下所示

cl_label = "kNN"
knn_estimator = KNeighborsClassifier(algorithm='ball_tree', p=2, n_neighbors=5)
knn_estimator.fit(self.data_train, self.target_train)
self.roc_estimators[cl_label] = knn_estimator

和 分别为最近质心

cl_label = "Nearest Centroid"
nc_estimator = NearestCentroid(metric='euclidean', shrink_threshold=6)
nc_estimator.fit(self.data_train, self.target_train)
self.roc_estimators[cl_label] = nc_estimator

所以它适用于我尝试过的所有分类器,但不适用于最近的质心。关于我所缺少的最近质心分类器的性质,是否存在特定原因,这解释了为什么无法绘制 ROC 曲线(更具体地说,为什么估计器没有 .predict_proba()方法?)提前谢谢您!

最佳答案

您需要为每个预测提供一个“分数”才能绘制 ROC 曲线。这可能是属于某一类别的预测概率。

参见例如https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Curves_in_ROC_space

只要寻找最近的质心就会给你预测的类别,但不是概率。

编辑:对于 NearestCentroid,无法计算分数。这只是模型的限制。它为每个样本分配一个类别,但不分配该类别的概率。我想如果你需要使用最近质心并且想要一个概率,你可以使用一些集成方法。训练训练数据子集的一堆模型,并在测试集上平均它们的预测。这可以给你一个分数。请参阅 scikit-learn.org/stable/modules/ensemble.html#bagging

关于python - 绘制最近质心的 ROC 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47572380/

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