#coding=utf-8
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import f1_score
y_true = np.array([[1, 0, 0],
[1, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 0],
[0, 0, 0],
[0, 0, 0]])
# If you change any of the following arrays to a number other than 0 and 1, you # will get an error.
y_pred = np.array([[1, 0, 0],
[1, 1, 0],
[0, 2, 1], ####error
[1, 0, 1],
[0, 1, 0],
[0, 0, 0],
[0, 0, 0]])
print (f1_score(y_true, y_pred, average='micro'))
#The following situation will arbitrarily modify the number, no error will be #reported.
z_true = np.array([1, 2, 3, 4, 3])
z_pred = np.array([1, 0, 2, 4, 3])
print (f1_score(z_true, z_pred , average='micro'))
#
此问题与其他类似错误不同。
#############################################################
Traceback (most recent call last):
File "D:/PycharmWorkSpace/ceshi/GridSearch/Grid Search.py", line 20, in <module>
print (f1_score(y_true, y_pred, average='micro'))
File "D:\python(2.7.9)\lib\site-packages\sklearn\metrics\classification.py", line 639, in f1_score
sample_weight=sample_weight)
File "D:\python(2.7.9)\lib\site-packages\sklearn\metrics\classification.py", line 756, in fbeta_score
sample_weight=sample_weight)
File "D:\python(2.7.9)\lib\site-packages\sklearn\metrics\classification.py", line 956, in precision_recall_fscore_support
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "D:\python(2.7.9)\lib\site-packages\sklearn\metrics\classification.py", line 82, in _check_targets
"".format(type_true, type_pred))
ValueError: Can't handle mix of multilabel-indicator and multiclass-multioutput
答案 0 :(得分:1)
在y_pred中将1更改为2是什么意思?对于多标签y_pred,应该使用一种热编码,其中每一列表示一个类,并且每个条目仅占0/1。 该错误源于将二进制编码与类标签混淆。