我有以下模型:
model.get_params()
{'boosting_type': 'rf', 'class_weight': None, 'colsample_bytree': 1, 'importance_type': 'split',
'learning_rate': 0.5,'max_depth': 2,'min_child_samples': 20, 'min_child_weight': 0.001,
'min_split_gain': 0.0, 'n_estimators': 450,'n_jobs': -1,'num_leaves': 5, 'objective': 'binary',
'random_state': 501, 'reg_alpha': 3, 'reg_lambda': 0.6, 'silent': True, 'subsample': 0.1,
'subsample_for_bin': 200000, 'subsample_freq': 0, 'bagging_fraction': 0.1, 'bagging_freq': 1,
'metric': 'custom'}
当我尝试使用业务指标重新训练它时:
def custom_asymmetric_train( y_true, y_pred, ):
y_true = np.array(y_true, dtype=int)
function=np.where((y_pred>0.5)&(y_true==0), -1000, 0)
function2=np.where((y_pred>0.5)&(y_true==1), 1, 0)
res=np.mean(function+function2).astype(float)
return res
mdl_metric = lgbm.LGBMClassifier()
mdl_metric.fit(
X_train, y_train,
eval_set=[(X_test, y_test)],
#eval_metric= lambda y_true, y_pred: [custom_asymmetric_train(y_true, y_pred)],
eval_metric=custom_asymmetric_train,
verbose=5,
init_model=model
)
发生错误:
TypeError: cannot unpack non-iterable numpy.float64 object
从文档:
你从函数返回一个值而不是所有这些,这就是错误出现的地方。从函数返回元组/格式列表
(eval_name, eval_result, is_higher_better)