【发布时间】:2017-09-25 16:15:17
【问题描述】:
我有一个多类分类任务。当我基于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_pred和y_test作为 one-hot 编码数组?你原来的类标签是什么?你应该给出你的代码,从你如何转换你的y开始。 -
@VivekKumar 我将
y_train和y_test二进制化为y_test = label_binarize(y_test, classes=[0, 1, 2, 3, 4, 5])为OneVsRestClassifier()。 -
您应该将原始类(未二进制化)放入
confusion_matrix。您需要反向转换您的y_pred以从中获取原始类。 -
@VivekKumar 谢谢。我使用了非二值化版本,它解决了。
标签: python scikit-learn classification confusion-matrix