【发布时间】:2018-05-14 07:39:06
【问题描述】:
我想绘制一条 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() 方法?)提前谢谢你!
【问题讨论】:
标签: python matplotlib plot scikit-learn