target is multiclass but average='binary'. please choose another average setting.

引用sklearn模型,fit之后进行模型评估时,出现该错误,代码如下:

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import f1_score

regressor = DecisionTreeClassifier(random_state=42)
regressor.fit(X_train,y_train)
y_pred = regressor.predict(X_test)

print(f1_score(y_pred,y_test))

运行后出现:

target is multiclass but average='binary'. please choose another average setting.

代码其实没有问题,问题出在数据类型上,这里的y_test中的数值并非是0、1二元,而是[123,23,142,243……],所以本质上来说,无法进行发f1_score的计算,你会发现,倘若将f1_score换为accuracy_score时,返回结果为0
所以对此类数值类型的y进行预测,需要其他评估指标,比如R²,代码如下:

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import r2_score
regressor = DecisionTreeClassifier(random_state=42)
regressor.fit(X_train,y_train)

# TODO:输出在测试集上的预测得分
y_pred = regressor.predict(X_test)

print(r2_score(y_pred,y_test))

结果:

0.12800402751210926

你可能感兴趣的:(机器学习,python,binary,f1score,sklearn,average)