text_plain_output_1.png
from sklearn.metrics import mean_squared_error

import lightgbm as lgbm

import numpy as np

import lightgbm as lgbm
from sklearn.metrics import mean_squared_error
def fit_lgbm(X, y, cv, params: dict=None, verbose: int=50):
    if params is None:
        params = {}
    models = []
    oof_pred = np.zeros_like(y, dtype=np.float)
    for i, (idx_train, idx_valid) in enumerate(cv):
        x_train, y_train = (X[idx_train], y[idx_train])
        x_valid, y_valid = (X[idx_valid], y[idx_valid])
        clf = lgbm.LGBMRegressor(**params)
        with Timer(prefix='fit fold={} '.format(i)):
            clf.fit(x_train, y_train, eval_set=[(x_valid, y_valid)], early_stopping_rounds=100, verbose=verbose)
        pred_i = clf.predict(x_valid)
        oof_pred[idx_valid] = pred_i
        models.append(clf)
        print(f'Fold {i} RMSLE: {mean_squared_error(y_valid, pred_i) ** 0.5:.4f}')
        print()
    score = mean_squared_error(y, oof_pred) ** 0.5
    print('-' * 50)
    print('FINISHED | Whole RMSLE: {:.4f}'.format(score))
    return (oof_pred, models)