我在 MATLAB 中有两个矩阵。每一个都在不同的位置填充1
和0
。我想比较每个元素:
- 如果有
1
匹配项,我希望将其记录为真阳性。 - 如果有
0
匹配项,我希望将其记录为 True Negative。 - 如果一个说
1
而另一个说0
,我想记录为误报。 - 如果一个说
0
,另一个说1
,我想记录为假阴性。
我尝试只比较两个矩阵:
idx = A == B
但是,这给了我一个简单的匹配,没有告诉我什么时候有真阳性或阴性等。
是否有任何我可以使用的特定功能或任何替代功能?
最佳答案
您可以按规定的方式添加矩阵....
a = [1 0 1 0
1 1 0 0
0 0 1 1];
b = [1 0 0 0
0 0 0 1
0 0 1 0];
C = a + 2*b;
% For pairs [a,b] we expect
% [0,0]: C = 0, true negative
% [1,0]: C = 1, false positive
% [0,1]: C = 2, false negative
% [1,1]: C = 3, true positive
% C =
% [ 3 0 1 0
% 1 1 0 2
% 0 0 3 1 ]
如果您有统计和机器学习工具箱并且您只想要一个摘要,您可能只需要函数 confusionmat
。
来自文档:
C = confusionmat(group,grouphat)
returns the confusion matrixC
determined by the known and predicted groups ingroup
andgrouphat
. [...].C
is a square matrix with size equal to the total number of distinct elements ingroup
andgrouphat
.C(i,j)
is a count of observations known to be in groupi
but predicted to be in groupj
.
例如:
a = [1 0 1 0
1 1 0 0
0 0 1 1];
b = [1 0 0 0
0 0 0 1
0 0 1 0];
C = confusionmat( a(:), b(:) );
% C =
% [ 5 1
% 4 2]
% So for each pair [a,b], we have 5*[0,0], 2*[1,1], 4*[1,0], 1*[0,1]
对于那些使用神经网络工具箱的人来说,类似的功能是混淆
。
关于matlab - 如何创建比较两个矩阵的误差函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58914511/