scikit-learn - roc_auc_score 用于不平衡类
问题描述
我有一个y_true
并且y_pred
同时使用不平衡的数据集。数据集中的类总数为 5。同时,y_true
它的批次中可能有 2-5 个不同的类。
对于roc_auc_score
,y_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_true
和y_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?
解决方案
推荐阅读
- c# - 数据处理 OracleDataReader (OracleRefCursor) 的问题
- windows - 在 Windows 注册表中模拟 Python 安装
- reactjs - 如何处理阿波罗客户端上的突变错误?
- java - Keypress 不会触发我所有的社交按钮,只有一个?
- sql - case语句中的子查询
- mysql - 从 AMI 创建的 AWS EC2 实例不起作用(错误 504)- WEBb 不运行
- ruby-on-rails - 如果 Ruby on Rails 中已存在记录,则提供选项
- php - cakephp 允许传递给动作的参数中的点
- node.js - 如何在 nodejs 应用程序中动态导入数据?
- ios - 将按钮(UIView)绑定到可在多个文本字段中使用的键盘(Swift 4.2)