我在gridsearchcv中使用了自定义函数。功能是计算top2十分中的召回率。但是我在运行grid_search.fit()
时遇到以下错误:
TypeError:my_custom_func()缺少1个必需的位置参数: 'y'
代码如下:
xgb_params = [{
'n_estimators':[100],'learning_rate' : [0.1]
}]
xgb_model = xgb.XGBClassifier()
def my_custom_func(estimator, X, y):
pred_probs = estimator.predict_proba(X)[:, 1]
pred_probs_2 = ( pred_probs>np.percentile(pred_probs,80) ).astype(int)
return recall_score(y, pred_probs_2)*100
my_scorer = make_scorer(my_custom_func, greater_is_better=True, needs_proba = True)
grid_search = GridSearchCV(xgb_model,cv=3,param_grid= xgb_params, scoring=my_scorer)
grid_search.fit(x_train_dummy,y_train)