【问题标题】:Can't handle mix of multiclass and continuous无法处理多类和连续的混合
【发布时间】:2017-06-06 15:52:33
【问题描述】:

输出有四个类别:[0,1,2,3] 预测是[0,1]中的连续数(使用sigmoid函数后)

我在sklearn中试过混淆矩阵f1_score,但是两种情况都有错误:

ValueError: Can't handle mix of multiclass and continuous

如果我将其简化为二元分类器并使用 AUC 对其进行评估,则没有错误,这意味着 AUC 可以处理连续输入。

我的问题是我在哪里可以找到 sklearn 中的评估,这样不仅可以处理多类,还可以处理连续输入。

【问题讨论】:

  • 为了清楚起见 - 您正在尝试分类为 4 个类别之一,您的预测是每个类别的 [0,1] 范围内的数字?假设你得到这个输出——你如何选择分类的类?
  • 好问题。首先,它们在 [0,1] 中,因为我选择的激活是 sigmoid 函数。
  • 然后,绘制输出分布并选择三个阈值
  • 所以你得到一个 [0,1] 浮点数的 1X4 向量,选择 3 个阈值(如何?)然后呢?你如何使用阈值?
  • 我得到一个 [0,1] 的 1*n(数据集大小)数组。在 x-y 中绘制它们。 x 是它们的值,y 是特定 x 对应的项目数。然后选择四个峰值(中心极限定理)并在每个峰值之间找到三个阈值。(可能在每个峰值的中间)。我使用阈值来预测。

标签: machine-learning scikit-learn classification


【解决方案1】:

在处理问题的细节之前,您需要确保您了解 AUC 指标以及如何正确使用它。

要了解 AUC 指标的含义意味着,您可以开始 here。

本质上,您希望获得基于不同阈值的预测列表(即移动它们并每次都获得预测),计算每个阈值实例的误报率和误报率,然后计算它们的 AUC。

计算和评估多类 AUC 并不简单。你可以找到更多信息here,但我在下面附上了一个很好的代码 sn-p 让你开始。

# Compute macro-average ROC curve and ROC area

# First aggregate all false positive rates, 
# assuming fpr is a list of false positive values per class
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))

# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
    mean_tpr += interp(all_fpr, fpr[i], tpr[i])

# Finally average it and compute AUC
mean_tpr /= n_classes

fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

# Plot all ROC curves
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
     label='micro-average ROC curve (area = {0:0.2f})'
           ''.format(roc_auc["micro"]),
     color='deeppink', linestyle=':', linewidth=4)

plt.plot(fpr["macro"], tpr["macro"],
     label='macro-average ROC curve (area = {0:0.2f})'
           ''.format(roc_auc["macro"]),
     color='navy', linestyle=':', linewidth=4)

colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
for i, color in zip(range(n_classes), colors):
    plt.plot(fpr[i], tpr[i], color=color, lw=lw,
         label='ROC curve of class {0} (area = {1:0.2f})'
         ''.format(i, roc_auc[i]))

plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 2017-04-03
    • 2019-06-24
    • 2018-08-05
    • 2021-12-20
    • 2020-11-11
    • 2022-01-25
    • 2019-10-22
    • 2021-04-14
    相关资源
    最近更新 更多