我有以下代码来生成混淆矩阵,在其中生成热图和 accuracy_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 。有人可以帮我修改代码,以便向我显示正确的准确度得分。
此致
巴拉斯·维卡斯
答案 0 :(得分:0)
您不需要混淆矩阵来计算准确性。
试试:
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]