简体   繁体   English

正确绘制混淆矩阵

[英]Plotting confusion matrix correctly

import matplotlib.pyplot as plt
from keras.models import load_model


plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

I am getting error while trying to plot confusion matrix for the below code snippet尝试以下代码片段的 plot 混淆矩阵时出现错误

from sklearn.metrics import confusion_matrix
pred = model.predict(X_test)
pred = np.argmax(pred,axis = 1) 
y_true = np.argmax(y_test,axis = 1)

The error for the above snippet say "NameError: name 'model' is not defined"上述代码段的错误说“NameError:名称'模型'未定义”

CM = confusion_matrix(y_true, pred)
from mlxtend.plotting import plot_confusion_matrix
fig, ax = plot_confusion_matrix(conf_mat=CM ,  figsize=(5, 5))
plt.show()

The error for the above snippet says "NameError: name 'y_true' is not defined"上述代码段的错误是“NameError: name 'y_true' is not defined”

error image错误图像

y_true is not defined because the previous cell (where the model error occurs) does not complete. y_true未定义,因为前一个单元格(发生model错误的位置)未完成。 As you define y_true in the same cell where the model not defined error occurs, the cell doesn't execute the rest of the lines, hence y_true is not defined.当您在发生model not defined错误的同一单元格中定义y_true时,该单元格不会执行行的 rest,因此y_true

The first error then, model not defined is happening because you haven't assigned anything to the variable model .那么第一个错误, model not defined正在发生,因为您没有为变量model分配任何东西。 This should be fixed first ie :这应该首先修复,即:

model = load_model(<filepath to your saved model>)

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

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