Reputation: 299
How would I confirm the the columns/rows of an outputted Confusion Matrix if I didn't initially specify them when creating the matrix such as in the below code:
y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
cm=confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
[0, 0, 1],
[1, 0, 2]])
From the docs I know it says If none is given, those that appear at least once in y_true or y_pred are used in sorted order
so I would assume the columns/rows would be ("ant", "bird", "cat")
but how do I confirm that?
I tried something like cm.labels
but that doesn't work.
Upvotes: 1
Views: 662
Reputation: 18883
In the source code of the confusion_matrix:
if labels is None:
labels = unique_labels(y_true, y_pred)
What is unique_labels
and where is it imported from?
from sklearn.utils.multiclass import unique_labels
unique_labels(y_true, y_pred)
Returns
array(['ant', 'bird', 'cat'],
dtype='<U4')
unique_labels
extracts an ordered array of unique labels.
Examples:
>>> from sklearn.utils.multiclass import unique_labels
>>> unique_labels([3, 5, 5, 5, 7, 7])
array([3, 5, 7])
>>> unique_labels([1, 2, 3, 4], [2, 2, 3, 4])
array([1, 2, 3, 4])
>>> unique_labels([1, 2, 10], [5, 11])
array([ 1, 2, 5, 10, 11])
Maybe a more intuitive example:
unique_labels(['z', 'x', 'y'], ['a', 'z', 'c'], ['e', 'd', 'y'])
Returns:
array(['a', 'c', 'd', 'e', 'x', 'y', 'z'],
dtype='<U1')
Upvotes: 1