【问题标题】:ValueError: multiclass-multioutput format is not supported using sklearn roc_auc_score functionValueError:使用 sklearn roc_auc_score 函数不支持多类多输出格式
【发布时间】:2018-11-07 02:02:29
【问题描述】:

我正在使用logistic regression 进行预测。我的预测是0's1's。在给定数据训练我的模型之后,以及在训练重要特征时,例如X_important_train,请参见屏幕截图。我得到了大约 70% 的分数,但是当我使用 roc_auc_score(X,y)roc_auc_score(X_important_train, y_train) 时,我得到了值错误: ValueError: multiclass-multioutput format is not supported

代码:

# Load libraries
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_auc_score

# Standarize features
scaler = StandardScaler()
X_std = scaler.fit_transform(X)

# Train the model using the training sets and check score
model.fit(X, y)
model.score(X, y)

model.fit(X_important_train, y_train)
model.score(X_important_train, y_train)

roc_auc_score(X_important_train, y_train)

截图:

【问题讨论】:

  • X_important_train, y_train的形状是什么?
  • @seralouk X_important_train (51202, 7) 和 y_train (51202,)
  • 你应该得到模型的预测,然后在roc_auc_score中使用它们。你在做什么是不正确的。您使用 TRAINING 数据作为 roc_auc_score 中的输入 + 形状应该相同
  • @seralouk 是的,明白了。我应该使用roc_auc_score(y_test, y_important_pred)roc_auc_score(y_test, y_pred)。你能写一个答案吗?
  • 只是一个简单的问题......如果不告诉系统你是怎么做的,你将如何定义你的 roc?语法,正如您从错误 Traceback 和文档中看到的那样,roc_auc_score(y_true, y_score) 不对应于 roc_auc_score(X,y)

标签: python pandas scikit-learn logistic-regression


【解决方案1】:

首先,roc_auc_score 函数需要具有相同形状的输入参数。

sklearn.metrics.roc_auc_score(y_true, y_score, average=’macro’, sample_weight=None)

Note: this implementation is restricted to the binary classification task or multilabel classification task in label indicator format.

y_true : array, shape = [n_samples] or [n_samples, n_classes]
True binary labels in binary label indicators.

y_score : array, shape = [n_samples] or [n_samples, n_classes]
Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).

现在,输入是真实分数和预测分数,而不是您在发布的示例中使用的训练和标签数据。 更详细地说,

model.fit(X_important_train, y_train)
model.score(X_important_train, y_train)
# this is wrong here
roc_auc_score(X_important_train, y_train)

你应该这样:

y_pred = model.predict(X_test_data)
roc_auc_score(y_true, y_pred)

【讨论】:

    猜你喜欢
    • 2020-02-14
    • 2017-10-19
    • 2020-06-05
    • 2021-03-22
    • 2016-01-06
    • 2020-07-21
    • 2014-11-30
    • 2020-06-07
    相关资源
    最近更新 更多