首页 > 解决方案 > roc_auc_score 用于不平衡类

问题描述

我有一个y_true并且y_pred同时使用不平衡的数据集。数据集中的类总数为 5。同时,y_true它的批次中可能有 2-5 个不同的类。

对于roc_auc_scorey_pred必须是大小(n_samples, n_classes)。在这种情况下,那将是(n_samples, 6). 但是,由于y_true类可能较少,[0,2,4]例如,它的输入将具有唯一值。这会引发错误: Number of classes in y_true not equal to the number of columns in 'y_score'

y_truey_pred都是 typenp.ndarray时,我们得到以下结果:

y_true = y_true.astype('uint8')
y_pred_one_hot = np.zeros((y_pred.size, y_pred.max()+1))
y_pred_one_hot[np.arange(y_pred.size),y_pred] = 1

对于三个批次迭代,我们得到以下结果:

1. y_true shape = (614400,) y_true unique = [0 1 3 4 5] y_pred_one_hot shape = (614400, 5)
2. y_true shape = (614400,) y_true unique = [0 1 2 3 4] y_pred_one_hot shape = (614400, 5)
3. y_true shape = (614400,) y_true unique = [0 1 2 3 4 5] y_pred_one_hot shape = (614400, 5)

最后一批迭代将调用错误,因为形状实际上需要是 (614400, 6)。但是,将其设置为 (614400, 6) 会在前 2 次迭代中调用错误。并且由于 y_pred_one_hot 需要单热编码,我们不能考虑类的顺序。对于第一次迭代,[0 1 3 4 5] 将与第二次迭代的 [0 1 2 3 4] 相同。

如何将 roc_auc_score 的 one-hot-encoding 与这些不平衡的类一起使用?例如,当我有 y_true unique = [0,1,4] 时,我应该如何使用 y_pred?

标签: scikit-learnimbalanced-data

解决方案


推荐阅读