【问题标题】:Plotting confusion matrix correctly正确绘制混淆矩阵
【发布时间】:2021-04-23 07:10:39
【问题描述】:
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()

我在尝试为以下代码 sn-p 绘制混淆矩阵时遇到错误

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)

上述sn-p的错误是“NameError: name 'model' is not defined”

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()

上述 sn-p 的错误是“NameError: name 'y_true' is not defined”

error image

【问题讨论】:

  • 您好!作为本网站的长期用户,您的问题已出现在我的审核队列中。恐怕这有点像XY problem:您在问如何“绘制混淆矩阵”,但是您对错误消息的困惑表明您对 Python 的理解还不够好,无法将其用于 ML 任务. (要明白我的意思,请尝试从命令提示符运行它:python -c "print(model)"。)这里没有判断(不久前我自己还是个初学者),但我建议你在尝试使用之前阅读一本关于 Python 的介绍性书籍它适用于 ML。

标签: python keras deep-learning


【解决方案1】:

y_true 未定义,因为前一个单元格(发生model 错误的位置)未完成。当您在发生model not defined 错误的同一单元格中定义y_true 时,该单元格不会执行其余行,因此未定义y_true

第一个错误,model not defined 正在发生,因为您没有为变量 model 分配任何内容。这应该首先修复,即:

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

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 2016-06-04
    • 2019-11-23
    • 2017-10-17
    • 2021-10-07
    • 2013-10-25
    • 2020-07-15
    • 2019-12-26
    相关资源
    最近更新 更多