我正在做一个隔离森林,我想使用GridSearchCV优化我的超参数。我希望我的评分基于异常值的召回得分,即标签 = -1。但是,我在运行此代码时遇到错误。
recall_fraud = make_scorer(recall_score(pos_label=-1))
gs_params ={
'max_samples': [300,500,1000],
'contamination': [float(y_train.count(-1))/len(y_train)] ,
'max_features': [1,3,7],
'n_estimators':[1000],
'random_state':[1]
}
isof_gs = GridSearchCV(IsolationForest(), gs_params, n_jobs = 1, verbose = 1, cv = 5, scoring = recall_fraud)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-273-d1f260f73f29> in <module>()
----> 1 recall_fraud = make_scorer(recall_score(pos_label=-1))
2 gs_params ={
3 'max_samples': [300,500,1000],
4 'contamination': [float(y_train.count(-1))/len(y_train)] ,
5 'max_features': [1,3,7],
TypeError: recall_score() takes at least 2 arguments (1 given)
我做错了什么吗?
当使用make_scorer
时,所有你想要传递给评分函数的关键字参数都应该传递给make_scorer
,而不是内部评分函数。
看看make_scorer
中的kwargs
参数:-
**kwargs : 附加参数 要传递给score_func的其他参数。
像这样更改代码:
# Updated recall_score() to recall_score
recall_fraud = make_scorer(recall_score, pos_label=-1)
它不会再抛出错误了。
在实际计算网格搜索的召回率时,pos_label
将自动转发到 recall_score 方法。