arnaud
arnaud

Reputation: 3473

Why is my confusion matrix returning only one number?

I'm doing a binary classification. Whenever my prediction equals the ground truth, I find sklearn.metrics.confusion_matrix to return a single value. Isn't there a problem?

from sklearn.metrics import confusion_matrix
print(confusion_matrix([True, True], [True, True])
# [[2]]

I would expect something like:

[[2 0]
 [0 0]]

Upvotes: 10

Views: 4796

Answers (2)

arnaud
arnaud

Reputation: 3473

You should fill-in labels=[True, False]:

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_true=[True, True], y_pred=[True, True], labels=[True, False])
print(cm)

# [[2 0]
#  [0 0]]

Why?

From the docs, the output of confusion_matrix(y_true, y_pred) is:

C: ndarray of shape (n_classes, n_classes)

The variable n_classes is either:

  • guessed as the number of unique values in y_true or y_pred
  • taken from the length of optional parameters labels

In your case, because you did not fill in labels, the variable n_classes is guessed from the number of unique values in [True, True] which is 1. Hence the result.

Upvotes: 13

Sohaib Anwaar
Sohaib Anwaar

Reputation: 1547

You are getting 1 number because you didn't specify your labels. I think this code will help you out to get true_positives, true negative, precesion, recall, f1-score etc

Code

from sklearn import metrics

A     = [True, True]
B     = [True, True]
label = [True, False] 


# True positives and False Negatives
cm = metrics.confusion_matrix(y_true=A, y_pred=B, labels=label)
print(cm)

# Report (f1-score, precesion, recall, support)
report = metrics.classification_report([True, False], [False, True], digits=3,output_dict=True)
df = pd.DataFrame(report).transpose()
df

Results

[[2 0]
 [0 0]]

enter image description here

Upvotes: 0

Related Questions