有没有一种简单的方法来获得多类分类的混淆矩阵? (OneVsRest)
Is there an easy way to get confusion matrix for multiclass classification? (OneVsRest)
我在三个 class class化问题(三个随机森林)上使用 OneVsRest classifier。每个 class 的出现都定义了我的虚拟整数(1 代表出现,0 代表其他)。我想知道是否有一种简单的替代方法来创建混淆矩阵?正如我遇到的所有方法一样,采用 y_pred、y_train = array、shape = [n_samples] 形式的参数。理想情况下,我想要 y_pred、y_train = 数组、形状 = [n_samples、n_classes]
一些样本,类似于问题的结构:
y_train = np.array([(1,0,0), (1,0,0), (0,0,1), (1,0,0), (0,1,0)])
y_pred = np.array([(1,0,0), (0,1,0), (0,0,1), (0,1,0), (1,0,0)])
print(metrics.confusion_matrix(y_train, y_pred)
RETURNS:
不支持多标签指示器
我不知道你在想什么,因为你没有指定你正在寻找的输出,但你可以通过以下两种方式实现它:
1.One 每列混淆矩阵
In [1]:
for i in range(y_train.shape[1]):
print("Col {}".format(i))
print(metrics.confusion_matrix(y_train[:,i], y_pred[:,i]))
print("")
Out[1]:
Col 0
[[1 1]
[2 1]]
Col 1
[[2 2]
[1 0]]
Col 2
[[4 0]
[0 1]]
2.One 一共混淆矩阵
为此,我们将展平数组:
In [2]: print(metrics.confusion_matrix(y_train.flatten(), y_pred.flatten()))
Out[2]:
[[7 3]
[3 2]]
您可以像下面这样尝试一次获取所有详细信息。
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
这将为您提供如下内容:
array([[ 7, 0, 0, 0],
[ 0, 7, 0, 0],
[ 0, 1, 2, 4],
[ 0, 1, 0, 11]])
-这意味着正确预测了所有对角线。
我在三个 class class化问题(三个随机森林)上使用 OneVsRest classifier。每个 class 的出现都定义了我的虚拟整数(1 代表出现,0 代表其他)。我想知道是否有一种简单的替代方法来创建混淆矩阵?正如我遇到的所有方法一样,采用 y_pred、y_train = array、shape = [n_samples] 形式的参数。理想情况下,我想要 y_pred、y_train = 数组、形状 = [n_samples、n_classes]
一些样本,类似于问题的结构:
y_train = np.array([(1,0,0), (1,0,0), (0,0,1), (1,0,0), (0,1,0)])
y_pred = np.array([(1,0,0), (0,1,0), (0,0,1), (0,1,0), (1,0,0)])
print(metrics.confusion_matrix(y_train, y_pred)
RETURNS: 不支持多标签指示器
我不知道你在想什么,因为你没有指定你正在寻找的输出,但你可以通过以下两种方式实现它:
1.One 每列混淆矩阵
In [1]:
for i in range(y_train.shape[1]):
print("Col {}".format(i))
print(metrics.confusion_matrix(y_train[:,i], y_pred[:,i]))
print("")
Out[1]:
Col 0
[[1 1]
[2 1]]
Col 1
[[2 2]
[1 0]]
Col 2
[[4 0]
[0 1]]
2.One 一共混淆矩阵
为此,我们将展平数组:
In [2]: print(metrics.confusion_matrix(y_train.flatten(), y_pred.flatten()))
Out[2]:
[[7 3]
[3 2]]
您可以像下面这样尝试一次获取所有详细信息。
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
这将为您提供如下内容:
array([[ 7, 0, 0, 0],
[ 0, 7, 0, 0],
[ 0, 1, 2, 4],
[ 0, 1, 0, 11]])
-这意味着正确预测了所有对角线。