Reputation: 12747
My confusion matrix is showing up an error that I can't understand.
I want a confusion matrix to show the confusion between two arrays, y_pred
and y_test
.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, roc_curve, auc
from sklearn.metrics import accuracy_score
import pylab as pl
# Code that fills up two numpy arrays, y_test and y_pred with integers
print y_test.shape
print y_pred.shape
cm = confusion_matrix(y_test,y_pred)
plt.matshow(cm)
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
The error is:
Traceback (most recent call last):
File "C:\work_asaaki\code\test_samme_46_classes_unconfused.py", line 159, in <module>
cm = confusion_matrix(y_test,y_pred)
File "C:\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 742, in confusion_matrix
y_type, y_true, y_pred = _check_clf_targets(y_true, y_pred)
File "C:\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 115, in _check_clf_targets
"".format(type_true, type_pred))
ValueError: Can't handle mix of multiclass and unknown
What does the error mean?
When I print out y_pred.shape
and y_test.shape
, I get the same shape, (318L). The values of both arrays range between 0 and 29.
Upvotes: 1
Views: 7471
Reputation: 12747
Never mind, I found the answer, it was quite simple.
The problem was that in the code (not shown), I had populated y_pred as a numpy array using dtype=object
, liked this:
y_pred = np.array(pickle.load(file("PATH_TO_FILE")), dtype=object)
I removed the dtype=object
part and it worked fine.
Upvotes: 3