【问题标题】:Error : "Number of classes in y_true not equal to the number of columns in 'y_score'"错误:“y_true 中的类数不等于 'y_score' 中的列数”
【发布时间】:2021-03-27 05:36:37
【问题描述】:

我有一个不平衡的多类数据集,当我尝试计算 roc_auc_score 时出现此错误:ValueError: Number of classes in y_true not equal to the number of columns in 'y_score'。

代码如下:

model = svm.SVC(kernel='linear', probability=True)
model.fit(X_train, y_train)
y_prob = model.predict_proba(X_test)
macro_roc_auc_ovr = roc_auc_score(y_test, y_prob, multi_class="ovr",
                              average="macro")

任何解决此问题的建议。

谢谢

【问题讨论】:

  • stackoverflow.com/help/how-to-ask 是关于如何提出好问题的绝佳指南。特别是stackoverflow.com/help/minimal-reproducible-example 有数据代码和你发现的错误是什么
  • 欢迎您! y_testy_prob 的尺寸是多少?如果它们是numpy 数组,您应该可以调用y_test.shapey_prob.shape
  • y_test.shape = (3661, 38) 和 y_prob.shape = (3661, 40)
  • 我也有同样的问题。 @nesrine-bn 找到任何解决方案了吗?
  • 没有@Daniel Vilas-Boas,还没有

标签: python roc multiclass-classification imbalanced-data


【解决方案1】:

当我在每个折叠中没有至少一个类的示例时,这个问题发生在我身上。为了解决问题,我将 KFold 替换为 StratifiedKFold。

我认为这可能是您的拆分方法有问题。您可以要求对拆分进行分层,也可以将 stratify=y 传递给 train_test_split 方法(如果您正在使用该方法)

【讨论】:

  • 这可能是一个错误的补丁。想象一下我们不能做分层KFold(时间序列设置),在这里,如果你只是有不平衡的类,没有解决方法......
猜你喜欢
  • 2021-12-22
  • 2020-03-19
  • 2020-08-25
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多