text_plain_output_1.png
from lightgbm import LGBMRegressor

from sklearn import model_selection

from sklearn.metrics import mean_squared_error

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('../input/30-days-of-ml/train.csv')
test = pd.read_csv('../input/30-days-of-ml/test.csv')
sample_sub = pd.read_csv('../input/30-days-of-ml/sample_submission.csv')

categorical_cols = [col for col in train.columns if 'cat' in col]
new_train = pd.get_dummies(train, columns=categorical_cols, prefix_sep='_')
new_test = pd.get_dummies(test, columns=categorical_cols, prefix_sep='_')
new_test = new_test.drop('id', axis=1)

new_train['kfold'] = -1
kf = model_selection.KFold(n_splits=5, shuffle=True, random_state=47)
for k, (train_idx, valid_idx) in enumerate(kf.split(X=new_train)):
    new_train.loc[valid_idx, 'kfold'] = k

def train_test_data(df, fold):
    x_train = df[df.kfold != fold].reset_index(drop=True)
    x_valid = df[df.kfold == fold].reset_index(drop=True)
    y_train = x_train.target
    y_valid = x_valid.target
    x_train = x_train.drop(['id', 'target', 'kfold'], axis=1)
    x_valid = x_valid.drop(['id', 'target', 'kfold'], axis=1)
    return {'x_train': x_train, 'y_train': y_train, 'x_valid': x_valid, 'y_valid': y_valid}

def n_trees_get_models():
    models = dict()
    n_trees = [10, 50, 100, 500, 1000]
    for n in n_trees:
        models[str(n)] = LGBMRegressor(n_estimators=n)
    return models

def n_depth_get_models():
    models = dict()
    for i in range(1, 11):
        models[str(i)] = LGBMRegressor(max_depth=i, num_leaves=2 ** i)
    return models

def n_lr_get_models():
    models = dict()
    rates = [0.0001, 0.001, 0.01, 0.1, 1.0]
    for r in rates:
        key = '%.4f' % r
        models[key] = LGBMRegressor(learning_rate=r)
    return models

def n_boosting_types_get_models():
    models = dict()
    boosting_types = ['gbdt', 'dart', 'goss']
    for t in boosting_types:
        models[t] = LGBMRegressor(boosting_type=t)
    return models

for fold in range(5):
    datasets = train_test_data(new_train, fold)
    x_train = datasets['x_train']
    y_train = datasets['y_train']
    x_valid = datasets['x_valid']
    y_valid = datasets['y_valid']
    models = n_trees_get_models()
    print('************ FOLD: ' + str(fold + 1) + ' ************')
    for name, model in models.items():
        model.fit(x_train, y_train)
        preds = model.predict(x_valid)
        preds_test = model.predict(new_test)
        rmse = mean_squared_error(y_valid, preds, squared=False)
        print('For ' + str(name) + ' trees: ')
        print('RMSE Error for fold', fold + 1, ': ', rmse)