gpt4 book ai didi

classification - 如何打印混淆矩阵的标签和列名?

转载 作者:行者123 更新时间:2023-12-01 10:19:36 26 4
gpt4 key购买 nike

我得到了混淆矩阵,但由于我的实际数据集有很多分类类别,因此很难理解。

例子 -

>>> from sklearn.metrics import confusion_matrix
>>> y_test
['a', 'a', 'b', 'c', 'd', 'd', 'e', 'a', 'c']
>>> y_pred
['b', 'a', 'b', 'c', 'a', 'd', 'e', 'a', 'c']
>>>
>>>
>>> confusion_matrix(y_test, y_pred)
array([[2, 1, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 2, 0, 0],
[1, 0, 0, 1, 0],
[0, 0, 0, 0, 1]], dtype=int64)

但是如何打印标签/列名以便更好地理解?

我什至试过这个 -
>>> pd.factorize(y_test)
(array([0, 0, 1, 2, 3, 3, 4, 0, 2], dtype=int64), array(['a', 'b', 'c', 'd', 'e'], dtype=object))
>>> pd.factorize(y_pred)
(array([0, 1, 0, 2, 1, 3, 4, 1, 2], dtype=int64), array(['b', 'a', 'c', 'd', 'e'], dtype=object))

请问有什么帮助吗?

最佳答案

尝试这样的事情:

from sklearn.metrics import confusion_matrix
import pandas as pd
import numpy as np
y_test = ['a', 'a', 'b', 'c', 'd', 'd', 'e', 'a', 'c']
y_pred = ['b', 'a', 'b', 'c', 'a', 'd', 'e', 'a', 'c']


labels = np.unique(y_test)
a = confusion_matrix(y_test, y_pred, labels=labels)

pd.DataFrame(a, index=labels, columns=labels)

输出:
   a  b  c  d  e
a 2 1 0 0 0
b 0 1 0 0 0
c 0 0 2 0 0
d 1 0 0 1 0
e 0 0 0 0 1

关于classification - 如何打印混淆矩阵的标签和列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54875846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com