簡體   English   中英

如何比較兩個不同的 true 或 false 列並得到一個混淆矩陣? Python

[英]How to compare two different true or false columns and get a confusion matrix? Python

所以我有 2 個不同的 true 或 false 結果測試了同一列。 所以測試 1 的結果是錯誤的,而測試 2 的結果是正確的。 有沒有python代碼可以比較這兩個結果,得到一個混淆矩陣結果(true positives, false positives, false negatives, true negatives)?

例如:

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

您可以使用 numpy 執行此操作

我會忽略測試有字母的事實,而只使用數組

#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]
])

我不確定這是否是混淆矩陣的正確約定,但您可以在自己的代碼中重新排列它

附言:

也可以使用sklearn: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html

有沒有python代碼可以比較這兩個結果,得到一個混淆矩陣結果(true positives, false positives, false negatives, true negatives)?

假設Test1Test2是 Pandas 系列對象,

真陽性: Test1 & Test2

誤報: Test1 & (Test2 == False)

漏報: (Test1==False) & Test2

真否定: (Test1==False) & (Test2==False)

要獲取系列中真值的數量,請使用Series.count() ,例如,真陽性的數量將為(Test1 & Test2).count()

假設您希望將混淆矩陣作為 numpy 數組,您只需適當地填寫單元格:

confusion = np.zeros((2,2))
confusion[0,0] = (Test1 & Test2).count()

等等...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM