[英]How to compare two different true or false columns and get a confusion matrix? Python
So I have 2 different true or false results that tested the same column.所以我有 2 个不同的 true 或 false 结果测试了同一列。 So test 1 has the wrong results and test 2 has the correct results.
所以测试 1 的结果是错误的,而测试 2 的结果是正确的。 Is there python code that can compare these two results and obtain a confusion matrix result (true positives, false positives, false negatives, and true negatives)?
有没有python代码可以比较这两个结果,得到一个混淆矩阵结果(true positives, false positives, false negatives, true negatives)?
For example:例如:
Test1
a True
b True
c False
d False
e True
f True
g True
Test2
a True
b True
c True
d True
e True
f True
g False
You can do this with numpy您可以使用 numpy 执行此操作
I will ignore the fact that the tests have letters, and just use an array instead我会忽略测试有字母的事实,而只使用数组
#assume:
#reponses = [...list of booleans...]
#ground_truth = [...list of booleans...]
import numpy as np
responses = np.array(responses)
ground_truth = np.array(ground_truth)
true_positives = np.logical_and(responses,ground_truth)
true_negatives = np.logical_and(np.logical_not(responses),np.logical_not(ground_truth))
false_positives = np.logical_and(responses,np.logical_not(ground_truth))
false_negatives = np.logical_and(np.logical_not(responses),ground_truth)
num_true_positives = np.count_nonzero(true_positives)
num_true_negatives = np.count_nonzero(true_negatives)
num_false_positive = np.count_nonzero(false_positives)
num_false_negatives = np.count_nonzero(false_negatives)
confusion_matrix = np.array([
[num_true_positives,num_false_positives],
[num_true_negatives,num_false_negatives]
])
I'm not sure if that's the correct convention for the confusion matrix, but you can rearrange it in your own code我不确定这是否是混淆矩阵的正确约定,但您可以在自己的代码中重新排列它
PS:附言:
You can also use sklearn: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html也可以使用sklearn: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
Is there python code that can compare these two results and obtain a confusion matrix result (true positives, false positives, false negatives, and true negatives)?
有没有python代码可以比较这两个结果,得到一个混淆矩阵结果(true positives, false positives, false negatives, true negatives)?
Assuming Test1
and Test2
are Pandas Series objects,假设
Test1
和Test2
是 Pandas 系列对象,
True positives: Test1 & Test2
真阳性:
Test1 & Test2
False positives: Test1 & (Test2 == False)
误报:
Test1 & (Test2 == False)
False negatives: (Test1==False) & Test2
漏报:
(Test1==False) & Test2
True negatives: (Test1==False) & (Test2==False)
真否定:
(Test1==False) & (Test2==False)
To get the number of True values in a Series, use Series.count()
, For example, the number of true positives would be (Test1 & Test2).count()
.要获取系列中真值的数量,请使用
Series.count()
,例如,真阳性的数量将为(Test1 & Test2).count()
。
Assuming you want the confusion matrix as a numpy array, you just fill in the cells appropriately:假设您希望将混淆矩阵作为 numpy 数组,您只需适当地填写单元格:
confusion = np.zeros((2,2))
confusion[0,0] = (Test1 & Test2).count()
and so on...等等...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.