ValueError:目标是多类但平均值='二进制'。请选择另一个平均设置,[None, 'micro', 'macro', 'weighted']
Posted
技术标签:
【中文标题】ValueError:目标是多类但平均值=\'二进制\'。请选择另一个平均设置,[None, \'micro\', \'macro\', \'weighted\']【英文标题】:ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted']ValueError:目标是多类但平均值='二进制'。请选择另一个平均设置,[None, 'micro', 'macro', 'weighted'] 【发布时间】:2021-12-05 09:15:06 【问题描述】:我正在研究多类分类,我正在寻找分类器的准确性,我正在使用:
model = RandomForestClassifier(random_state=2)
model.fit(X_train, y_train)
preds = model.predict(X_test)
Accuracy=accuracy_score(y_test, preds, average='micro')
得到一个错误:
TypeError: accuracy_score() got an unexpected keyword argument 'average'
当我使用时:
model = RandomForestClassifier(random_state=2)
model.fit(X_train, y_train)
preds = model.predict(X_test)
Accuracy=accuracy_score(y_test, preds)
得到一个错误:
ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].
谁能帮我解决多类分类问题的准确性?
在我的 xgboost 函数中的代码下方:
scorers =
'f1_score':make_scorer(f1_score),
'precision_score': make_scorer(precision_score),
'recall_score': make_scorer(recall_score),
'accuracy_score': make_scorer(accuracy_score)
#fitting the training dataset to the model
xgb_model = XGBClassifier(n_jobs=-1, objective='multi:softmax')
#setattr(xgb_model, 'verbosity', 2)
param_dist = 'n_estimators': stats.randint(150, 1000),
'learning_rate': stats.uniform(0.01, 0.59),
'subsample': stats.uniform(0.3, 0.6),
'max_depth': [3, 4, 5, 6, 7, 8, 9],
'colsample_bytree': stats.uniform(0.5, 0.4),
'min_child_weight': [1, 2, 3, 4]
# numFolds = 5
# kfold_5 = cross_validation.KFold(n = len(X), shuffle = True, n_folds = numFolds)
skf = StratifiedKFold(n_splits=3, shuffle = True)
gridCV = RandomizedSearchCV(xgb_model,
param_distributions = param_dist,
cv = skf,
n_iter = 5,
scoring = scorers,
verbose = 3,
n_jobs = -1,
return_train_score=True,
refit = False)
gridCV.fit(x_train,y_train)
这是尝试make_scorer(f1_score,average='micro')
时的错误:
TypeError Traceback (most recent call last)
<ipython-input-68-8b24047fa926> in <module>
1 print("********** Xgboost classifier *************")
2 start_time = time.monotonic()
----> 3 y_test, xgb_predict, xgb_pred_prob = xgboost_classifier(x,y)
4 end_time = time.monotonic()
5 print(timedelta(seconds=end_time - start_time))
<ipython-input-67-2661dd9c3c1a> in xgboost_classifier(x, y)
36 scorers =
37 'f1_score':make_scorer(f1_score,average='micro'),
---> 38 'precision_score': make_scorer(precision_score()),
39 'recall_score': make_scorer(recall_score()),
40 'accuracy_score': make_scorer(accuracy_score())
TypeError: precision_score() missing 2 required positional arguments: 'y_true' and 'y_pred'
我不知道为什么gridCV.fit(x_train, y_train)
不向得分手提供Y
值?
【问题讨论】:
accuracy_score
应该没有关键字参数average
,这是真的。您确定您的第二条错误消息来自accuracy_score
函数吗?如果您有多个类并且忘记设置average 关键字,我预计会出现类似F1_score
的错误。您使用的是哪个版本的sklearn
?你能给我们整个错误堆栈输出吗?
这是我正在使用的整个代码,我已经准确地测试过它是否来自它或者没有 Sklearn 版本是 0.22.1
【参考方案1】:
我通过将平均值添加到 F1、精度和召回率来解决了这个问题。只有准确性不需要这个参数!
scorers =
'f1_score': make_scorer(f1_score, average='micro'),
'precision_score': make_scorer(precision_score, average='micro'),
'recall_score': make_scorer(recall_score, average='micro'),
'accuracy_score': make_scorer(accuracy_score)
【讨论】:
编辑您的问题。本部分用于答案。该帖子将被删除,因为在答案部分发布非答案是违反网站规则的。 谢谢,如果您想为您的问题添加一些内容,请使用“编辑”功能。 这可能是问题所在:make_scorer(f1_score)
。您还必须将任何关键字参数传递给make_scorer
。你可以试试make_scorer(f1_score,average='micro')
。
谢谢,我无法编辑我的问题,这就是我再次发布的原因。以上是关于ValueError:目标是多类但平均值='二进制'。请选择另一个平均设置,[None, 'micro', 'macro', 'weighted']的主要内容,如果未能解决你的问题,请参考以下文章
如何处理 ValueError:分类指标无法处理多标签指标和多类目标错误的混合