混淆矩阵查询

Confusion Matrix query

我有下面的代码来生成混淆矩阵,它在其中生成 heatmapaccuracy_score

来源

代码

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)

输出

从上面的输出中你可以看到百分比精度是:0.0正确的计数是:0但是它必须是百分比精度是:0.3正确计数是:3。谁能帮我修改一下代码,让它显示正确的 准确度得分.

此致,

巴拉斯·维卡斯

您不需要混淆矩阵来计算准确度。
尝试:

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

如果你真的想使用你的混淆矩阵,你可以这样做:

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]