gpt4 book ai didi

python - 如何在 Scikit 中计算多类分类的混淆矩阵?

转载 作者:太空狗 更新时间:2023-10-29 20:44:38 25 4
gpt4 key购买 nike

我有一个多类分类任务。当我基于 scikit example 运行我的脚本时如下:

classifier = OneVsRestClassifier(GradientBoostingClassifier(n_estimators=70, max_depth=3, learning_rate=.02))

y_pred = classifier.fit(X_train, y_train).predict(X_test)
cnf_matrix = confusion_matrix(y_test, y_pred)

我收到这个错误:

File "C:\ProgramData\Anaconda2\lib\site-packages\sklearn\metrics\classification.py", line 242, in confusion_matrix
raise ValueError("%s is not supported" % y_type)
ValueError: multilabel-indicator is not supported

我试图将 labels=classifier.classes_ 传递给 confusion_matrix(),但没有帮助。

y_test 和 y_pred 如下:

y_test =
array([[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0],
...,
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0]])


y_pred =
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
...,
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0]])

最佳答案

这对我有用:

y_test_non_category = [ np.argmax(t) for t in y_test ]
y_predict_non_category = [ np.argmax(t) for t in y_predict ]

from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test_non_category, y_predict_non_category)

其中 y_testy_predict 是分类变量,如 one-hot 向量。

关于python - 如何在 Scikit 中计算多类分类的混淆矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43665041/

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