3

I was trying to create roc curve for multiclass using Naive Bayes But it ending with

ValueError: bad input shape.

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)

raise ValueError("bad input shape {0}".format(shape))

ValueError: bad input shape (75, 6)

Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
Dipta Das
  • 402
  • 3
  • 8

1 Answers1

0

The error because of binarizing the y variable. The estimator can work with string values itself.

Remove the following lines,

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

You are good to go!

To get the predicted probabilities for roc_curve, use the following:

classifier.fit(X_train, y_train)
y_score = classifier.predict_proba(X_test)
y_score.shape
# (75, 3)
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77