Python panada.crosstab不';当列接收到相同的值时,t创建方表
我有一个创建混淆矩阵的函数Python panada.crosstab不';当列接收到相同的值时,t创建方表,python,pandas,machine-learning,confusion-matrix,Python,Pandas,Machine Learning,Confusion Matrix,我有一个创建混淆矩阵的函数 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'],
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,0,0],则混淆矩阵将不是正方形4x4,而只是4x1
Predicted 0
Actual
0 1
1 1
2 1
3 1
我如何填充没有得到任何预测值的其他列?(例如,此处第1、2和3列)如果需要所有可能的值,则应添加:
confusion_matrix = confusion_matrix.reindex(confusion_matrix.index, axis=1, fill_value=0)