Дообучение с бизнес метрикой

У меня следующая модель:

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

Ответы (1 шт):

Автор решения: CrazyElf

Из документации:

Custom eval function expects a callable with following signatures: func(y_true, y_pred), func(y_true, y_pred, weight) or func(y_true, y_pred, weight, group) and returns (eval_name, eval_result, is_higher_better) or list of (eval_name, eval_result, is_higher_better)

А вы возвращаете из функции единственное значение вместо всего этого, вот и выходит ошибка. Возвращайте из функции кортеж/cписок формата (eval_name, eval_result, is_higher_better)

→ Ссылка