小编Sha*_*kib的帖子

类数 4 与 target_names 的大小 6 不匹配。尝试指定 labels 参数

当我尝试制作 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
Run Code Online (Sandbox Code Playgroud)

我已经搜索过解决这个问题的方法,但仍然没有得到完美的解决方案。我在这个领域完全陌生,有人可以帮助我吗?谢谢。

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))
Run Code Online (Sandbox Code Playgroud)

python machine-learning confusion-matrix scikit-learn

8
推荐指数
1
解决办法
2万
查看次数