【发布时间】:2020-01-14 21:25:35
【问题描述】:
当我尝试制作我的 CNN 模型的混淆矩阵时遇到了一些问题。当我运行代码时,它会返回一些错误,例如:
print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))
Traceback (most recent call last):
File "<ipython-input-102-82d46efe536a>", line 1, in <module>
print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))
File "G:\anaconda_installation_file\lib\site-packages\sklearn\metrics\classification.py", line 1543, in classification_report
"parameter".format(len(labels), len(target_names))
ValueError: Number of classes, 4, does not match size of target_names, 6. Try specifying the labels parameter
我已经搜索过解决这个问题,但仍然没有得到完美的解决方案。 我是这个领域的新手,有人可以帮助我吗? 谢谢。
from sklearn.metrics import classification_report,confusion_matrix
import itertools
Y_pred = model.predict(X_test)
print(Y_pred)
y_pred = np.argmax(Y_pred, axis=1)
print(y_pred)
target_names = ['class 0(cardboard)', 'class 1(glass)', 'class 2(metal)','class 3(paper)', 'class 4(plastic)','class 5(trash)']
print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))
【问题讨论】:
-
欢迎使用 SO,它不通过按原样抛出我们所有的代码来工作; 在错误之后出现的代码是多余的并且与问题无关(它永远不会执行)并且只会增加不必要的混乱(已删除)。请参阅How to create a Minimal, Complete, and Verifiable example。
标签: python machine-learning scikit-learn confusion-matrix