简体   繁体   English

混淆矩阵查询

[英]Confusion Matrix query

I have the below code to generate the confusion Matrix where it generates heatmap and accuracy_score我有下面的代码来生成混淆矩阵,它生成热图accuracy_score

SOURCE来源

在此处输入图片说明

CODE代码

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn import metrics

data = pd.read_excel(r"\Confusion Matrix.xlsx")

df = pd.DataFrame(data)
confusion_matrix = pd.crosstab(df['Actual'], df['Pred'], rownames=['Actual'], colnames=['Predicted'], margins = True)

sn.heatmap(confusion_matrix, annot=True)
plt.show()

accuray_score_in_percentage = accuracy_score(['Actual'], ['Pred'])
accuray_score_in_count = accuracy_score(['Actual'], ['Pred'], normalize=False)

print('The Precentage Accuracy is : ', accuray_score_in_percentage)
print('The Count of corrects are : ', accuray_score_in_count)

OUTPUT输出

在此处输入图片说明

From the above output you can see The Precentage Accuracy is : 0.0 and The Count of corrects are : 0 but it has to be The Precentage Accuracy is : 0.3 and The Count of corrects are : 3 .从上面的输出中,您可以看到Precentage Accuracy is : 0.0 and The Count of Corrects are: 0但它必须是The Precentage Accuracy is: 0.3 and The Count of Corrects are: 3 Can some one help me to modify the code so it shows me the correct Accuracy Scores .有人可以帮我修改代码,以便它向我显示正确的准确度分数

Regards,问候,

Bharath Vikas巴拉特维卡斯

You don't need the confusion matrix to compute accuracy.您不需要混淆矩阵来计算准确性。
Try :尝试 :

accuray_score_in_percentage = accuracy_score(df['Actual'], df['Pred'])  
accuray_score_in_count = accuracy_score(df['Actual'], df['Pred'], normalize=False)

If you really want to use your confusion matrix, you can do :如果你真的想使用你的混淆矩阵,你可以这样做:

accuray_score_in_percentage = (confusion_matrix.loc[0,0]+confusion_matrix.loc[1,1])/confusion_matrix.loc["All","All"]
accuray_score_in_count = confusion_matrix.loc[0,0]+confusion_matrix.loc[1,1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM