我试图使用朴素贝叶斯为多类创建roc曲线,但是结尾是


  ValueError:输入形状错误。


import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle

from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.naive_bayes import BernoulliNB

from scipy import interp

# Import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

# Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]

# shuffle and split training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
                                                    random_state=0)

# Learn to predict each class against the other
classifier = BernoulliNB(alpha=1.0, binarize=6, class_prior=None, fit_prior=True)
y_score = classifier.fit(X_train, y_train).predict(X_test)



  引发ValueError(“错误的输入形状{0}”。format(shape))
  
  ValueError:输入形状错误(75,6)

最佳答案

由于二进制化y变量而导致的错误。估计器本身可以使用字符串值。

删除以下几行,

y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]


你已准备好出发!

要获取roc_curve的预测概率,请使用以下命令:

classifier.fit(X_train, y_train)
y_score = classifier.predict_proba(X_test)
y_score.shape
# (75, 3)

08-24 22:19