Ged*_*kas 6 python numpy pandas scikit-learn multilabel-classification
我在三类分类问题上使用OneVsRest分类器(三个随机森林).每个类的出现都是我的虚拟整数(1表示发生,0表示否则).我想知道是否有一种简单的替代方法来创建混淆矩阵?正如我遇到的所有方法一样,以y_pred,y_train = array,shape = [n_samples]的形式获取参数.理想情况下,我想要y_pred,y_train = array,shape = [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)
Run Code Online (Sandbox Code Playgroud)
退货:不支持多标签指标
我不知道你有什么想法,因为你没有指定你想要的输出,但是有两种方法可以解决它:
1.每列一个混淆矩阵
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]]
Run Code Online (Sandbox Code Playgroud)
2.一个混淆矩阵
为此,我们将展平数组:
In [2]: print(metrics.confusion_matrix(y_train.flatten(), y_pred.flatten()))
Out[2]:
[[7 3]
[3 2]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3828 次 |
| 最近记录: |