[英]Precision score error: Classification metrics can't handle a mix of multiclass and continuous targets
I am currently in the process of trying to optimize a model for a certain performance metric using Hyperparameter tuning.我目前正在尝试使用超参数调整针对特定性能指标优化 model。 I did not receive the above error until I tried to tune my model specifically for the performance_score metric, when I just used my model as a normal predictor everything ran very smoothly, even when I added hyperparameter tuning it was fine.在我尝试专门针对 performance_score 指标调整 model 之前,我没有收到上述错误,当我刚刚使用 model 作为正常预测器时,一切都运行得非常顺利,即使我添加了超参数调整也很好。 Only when I tried to optimize it to a certain metric did it throw this error.只有当我尝试将其优化到某个指标时,它才会引发此错误。 This is the relevant code:这是相关代码:
scorers = {
'precision_score': make_scorer(precision_score),
'recall_score': make_scorer(recall_score),
'accuracy_score': make_scorer(accuracy_score)
}
rf = RandomForestRegressor()
cv=KFold(n_splits=5,random_state=1,shuffle=False)
rf_rsearch = RandomizedSearchCV(estimator = rf, param_distributions = random_hypparams, scoring=scorers,refit='precision_score', return_train_score=True, n_iter = 50, cv = cv , verbose=2, n_jobs = -1)
rf_rsearch.fit(OS_x, OS_y)
In this code:在这段代码中:
The error seems to be occurring in the final line of the chunk of code displayed.该错误似乎发生在显示的代码块的最后一行。 Any help would be really great as I cannot find anything online which is similar to this problem.任何帮助都会非常棒,因为我在网上找不到与此问题类似的任何内容。
The error message indicates that the precision_score you are using does not support scoring the types of outputs/targets you are passing.错误消息表明您使用的precision_score 不支持对您传递的输出/目标类型进行评分。 Does this model make multiclass or continuous predictions?这个 model 是否进行多类或连续预测?
If this is a multiclass predictor, you should be able to optimize according to precision_score, but you need to define an average for the multiclass targets by passing one of 'micro', 'macro', 'samples', or 'weighted' to precision_score's average parameter as the default='binary (see documentation) .如果这是一个多类预测器,您应该能够根据precision_score进行优化,但是您需要通过将“micro”、“macro”、“samples”或“weighted”之一传递给precision_score来定义多类目标的平均值平均参数作为 default='binary (参见文档) 。 The accepted answer to this StackOverflow post should help you do that with make_scorer. 这个 StackOverflow 帖子的公认答案应该可以帮助您使用 make_scorer 做到这一点。
If your model is making continuous predictions you will need to select a scorer that supports continuous output/target evaluation.如果您的 model 正在进行连续预测,您将需要 select 一个支持连续输出/目标评估的记分器。 The metrics under "Regression" in the scikit-learn documentation may help. scikit-learn 文档中“回归”下的指标可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.