简体   繁体   English

如何比较两个不同的 true 或 false 列并得到一个混淆矩阵? Python

[英]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,假设Test1Test2是 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.

相关问题 如何比较数据框中的两个字符串列表以获取任何匹配项以在 python 中获得 True 或 False? - How to compare two lists of strings in a dataframe for any matches to get a True or False in python? 如何在python中比较两列以获取重复项 - How to compare two columns to get duplicates in python 比较 2 个 csv 文件以获得不同 csv 文件中的结果与 python 中的 True 或 False 结果 - Compare of 2 csv files to get the result in a different csv file with the result of True or False in python 我如何比较python中两行不同的两列 - How can i compare two columns in two different rows in python 我想计算数据框中两列之间有多少不同的值用于混淆矩阵 - I want to count how many different value between two columns in dataframe for confusion matrix 如何比较python中两个不同数据框的列? - how to compare columns of two different data frames in python? 如何比较 Python 中两个数据帧的不同列 - How to compare different columns from two Dataframes in Python 如何使用正则表达式匹配来自不同数据帧的两列返回真/假? - How to match two columns from different dataframes returning True/False using regex? 如何根据True和False语句比较两个numpy数组 - How to compare two numpy array according to True and False statements 如何比较 SQL 文件和 Python 中的包并返回 True 或 False? - How to compare SQL files and Packages in Python and return True or False?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM