如果我在创建矩阵时没有最初指定矩阵(例如在以下代码中),我将如何确认输出混淆矩阵的列/行:
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]])
我知道的文档中说If none is given, those that appear at least once in y_true or y_pred are used in sorted order
,所以我假设列/行是("ant", "bird", "cat")
,但是我该如何确认?我尝试了类似cm.labels
的东西,但这不起作用。
在Confusion_matrix的源代码中:
if labels is None:
labels = unique_labels(y_true, y_pred)
什么是unique_labels
,从哪里导入?
from sklearn.utils.multiclass import unique_labels
unique_labels(y_true, y_pred)
返回
array(['ant', 'bird', 'cat'],
dtype='<U4')
unique_labels
提取一个唯一标签的有序阵列。
示例:
>>> 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])
也许是一个更直观的示例:
unique_labels(['z', 'x', 'y'], ['a', 'z', 'c'], ['e', 'd', 'y'])
返回:
array(['a', 'c', 'd', 'e', 'x', 'y', 'z'],
dtype='<U1')