问题描述
我具有创建混淆矩阵的功能
def disp_conf_mat(y_act,y_pred,conf_mat_name):
data = {'y_Actual': y_act,'y_Predicted': y_pred
}
df = pd.DataFrame(data,columns=['y_Actual','y_Predicted'])
confusion_matrix = pd.crosstab(df['y_Actual'],df['y_Predicted'],rownames=['Actual'],colnames=['Predicted'])
sn.heatmap(confusion_matrix,annot=True)
plt.savefig(conf_mat_name)
plt.close()
但是,如果实际值为[0,1,2,3]且预测的全为零[0,0],那么confusion_matrix不会是4x4的平方,而是4x1。
Predicted 0
Actual
0 1
1 1
2 1
3 1
如何填充没有任何预测值的其他列? (例如,此处的第1,2和3列)
解决方法
期望,如果需要所有可能的值,请添加DataFrame.reindex
:
confusion_matrix = confusion_matrix.reindex(confusion_matrix.index,axis=1,fill_value=0)