# -*- coding: utf-8 -*-
"""
Created on Thu Jul 11 21:43:42 2019

@author: Miguel
"""
def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    grad = np.where(residual<0, -2*10.0*residual, -2*residual)
    hess = np.where(residual<0, 2*10.0, 2.0)
    return grad, hess

def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 
    return "custom_asymmetric_eval", np.mean(loss), False

import lightgbm


# default lightgbm model with sklearn api
gbm = lightgbm.LGBMRegressor() 

# updating objective function to custom
# default is "regression"
# also adding metrics to check different scores
gbm.set_params(**{'objective': custom_asymmetric_train}, metrics = ["mse", 'mae'])

# fitting model 
gbm.fit(
    X_train,
    y_train,
    eval_set=[(X_valid, y_valid)],
    eval_metric=custom_asymmetric_valid,
    verbose=False,
)

y_pred = gbm.predict(X_valid)

 
# create dataset for lightgbm
# if you want to re-use data, remember to set free_raw_data=False
lgb_train = lgb.Dataset(X_train, y_train, free_raw_data=False)
lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train, free_raw_data=False)

# specify your configurations as a dict
params = {
    'objective': 'regression',
    'verbose': 0
}

gbm = lgb.train(params,
                lgb_train,
                num_boost_round=10,
                init_model=gbm,
                fobj=custom_asymmetric_train,
                feval=custom_asymmetric_valid,
                valid_sets=lgb_eval)
                
y_pred = gbm.predict(X_valid)

fff19af72abcb0d44c42e4876c9d529820a2100c
<mask token>


def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    grad = np.where(residual < 0, -2 * 10.0 * residual, -2 * residual)
    hess = np.where(residual < 0, 2 * 10.0, 2.0)
    return grad, hess


def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    loss = np.where(residual < 0, residual ** 2 * 10.0, residual ** 2)
    return 'custom_asymmetric_eval', np.mean(loss), False


<mask token>

<mask token>


def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    grad = np.where(residual < 0, -2 * 10.0 * residual, -2 * residual)
    hess = np.where(residual < 0, 2 * 10.0, 2.0)
    return grad, hess


def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    loss = np.where(residual < 0, residual ** 2 * 10.0, residual ** 2)
    return 'custom_asymmetric_eval', np.mean(loss), False


<mask token>
gbm.set_params(**{'objective': custom_asymmetric_train}, metrics=['mse', 'mae']
    )
gbm.fit(X_train, y_train, eval_set=[(X_valid, y_valid)], eval_metric=
    custom_asymmetric_valid, verbose=False)
<mask token>

<mask token>


def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    grad = np.where(residual < 0, -2 * 10.0 * residual, -2 * residual)
    hess = np.where(residual < 0, 2 * 10.0, 2.0)
    return grad, hess


def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    loss = np.where(residual < 0, residual ** 2 * 10.0, residual ** 2)
    return 'custom_asymmetric_eval', np.mean(loss), False


<mask token>
gbm = lightgbm.LGBMRegressor()
gbm.set_params(**{'objective': custom_asymmetric_train}, metrics=['mse', 'mae']
    )
gbm.fit(X_train, y_train, eval_set=[(X_valid, y_valid)], eval_metric=
    custom_asymmetric_valid, verbose=False)
y_pred = gbm.predict(X_valid)
lgb_train = lgb.Dataset(X_train, y_train, free_raw_data=False)
lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train, free_raw_data
    =False)
params = {'objective': 'regression', 'verbose': 0}
gbm = lgb.train(params, lgb_train, num_boost_round=10, init_model=gbm, fobj
    =custom_asymmetric_train, feval=custom_asymmetric_valid, valid_sets=
    lgb_eval)
y_pred = gbm.predict(X_valid)

<mask token>


def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    grad = np.where(residual < 0, -2 * 10.0 * residual, -2 * residual)
    hess = np.where(residual < 0, 2 * 10.0, 2.0)
    return grad, hess


def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype('float')
    loss = np.where(residual < 0, residual ** 2 * 10.0, residual ** 2)
    return 'custom_asymmetric_eval', np.mean(loss), False


import lightgbm
gbm = lightgbm.LGBMRegressor()
gbm.set_params(**{'objective': custom_asymmetric_train}, metrics=['mse', 'mae']
    )
gbm.fit(X_train, y_train, eval_set=[(X_valid, y_valid)], eval_metric=
    custom_asymmetric_valid, verbose=False)
y_pred = gbm.predict(X_valid)
lgb_train = lgb.Dataset(X_train, y_train, free_raw_data=False)
lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train, free_raw_data
    =False)
params = {'objective': 'regression', 'verbose': 0}
gbm = lgb.train(params, lgb_train, num_boost_round=10, init_model=gbm, fobj
    =custom_asymmetric_train, feval=custom_asymmetric_valid, valid_sets=
    lgb_eval)
y_pred = gbm.predict(X_valid)

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 11 21:43:42 2019

@author: Miguel
"""
def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    grad = np.where(residual<0, -2*10.0*residual, -2*residual)
    hess = np.where(residual<0, 2*10.0, 2.0)
    return grad, hess

def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 
    return "custom_asymmetric_eval", np.mean(loss), False

import lightgbm


# default lightgbm model with sklearn api
gbm = lightgbm.LGBMRegressor() 

# updating objective function to custom
# default is "regression"
# also adding metrics to check different scores
gbm.set_params(**{'objective': custom_asymmetric_train}, metrics = ["mse", 'mae'])

# fitting model 
gbm.fit(
    X_train,
    y_train,
    eval_set=[(X_valid, y_valid)],
    eval_metric=custom_asymmetric_valid,
    verbose=False,
)

y_pred = gbm.predict(X_valid)

 
# create dataset for lightgbm
# if you want to re-use data, remember to set free_raw_data=False
lgb_train = lgb.Dataset(X_train, y_train, free_raw_data=False)
lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train, free_raw_data=False)

# specify your configurations as a dict
params = {
    'objective': 'regression',
    'verbose': 0
}

gbm = lgb.train(params,
                lgb_train,
                num_boost_round=10,
                init_model=gbm,
                fobj=custom_asymmetric_train,
                feval=custom_asymmetric_valid,
                valid_sets=lgb_eval)
                
y_pred = gbm.predict(X_valid)
