gpt4 book ai didi

python - 类数,4,与 target_names 的大小不匹配,6。尝试指定标签参数

转载 作者:行者123 更新时间:2023-12-03 19:24:33 29 4
gpt4 key购买 nike

当我尝试制作我的 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))

最佳答案

问题是您有 6 个标签名称:'class 0(cardboard)', 'class 1(glass)', 'class 2(metal)','class 3(paper)', 'class 4(plastic)','class 5(trash)'
但是当您打印时,您的混淆矩阵中只有 4 个类:print(y_pred) : 你会得到带有 0,1,2,3 的数字的东西或者当您print(y_test)您将从 0,1,2,3 获得号码,它应该有助于删除:
print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))
从您的代码中,不知何故您没有 6 个预测/测试类。

这里也是如何绘制混淆矩阵的示例:How can I plot a confusion matrix?

关于python - 类数,4,与 target_names 的大小不匹配,6。尝试指定标签参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57916221/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com