text_plain_output_1.png
import lightgbm as lgb

import numpy as np

import pandas as pd

MAX_PRED = 1000
MAX_ROUNDS = 2000

indir = '../input/preparing-data-ii/'
indir2 = '../input/favorita-grocery-sales-forecasting/'

X_test = pd.read_csv(indir + 'X_test.csv')
X_val = pd.read_csv(indir + 'X_val.csv')
X_train = pd.read_csv(indir + 'X_train.csv')
y_train = np.array(pd.read_csv(indir + 'y_train.csv'))
y_val = np.array(pd.read_csv(indir + 'y_val.csv'))
stores_items = pd.read_csv(indir + 'stores_items.csv', index_col=['store_nbr', 'item_nbr'])
test_ids = pd.read_csv(indir + 'test_ids.csv', parse_dates=['date']).set_index(['store_nbr', 'item_nbr', 'date'])

items = pd.read_csv(indir2 + 'items.csv').set_index('item_nbr')
items = items.reindex(stores_items.index.get_level_values(1))

params = {'num_leaves': 31, 'objective': 'regression', 'min_data_in_leaf': 200, 'learning_rate': 0.025, 'feature_fraction': 0.8, 'bagging_fraction': 0.8, 'bagging_freq': 2, 'metric': 'l2', 'num_threads': 4}

val_pred = []
test_pred = []
cate_vars = []
for i in range(16):
    print('=' * 50)
    print('Step %d' % (i + 1))
    print('=' * 50)
    dtrain = lgb.Dataset(X_train, label=y_train[:, i], categorical_feature=cate_vars, weight=pd.concat([items['perishable']] * 6) * 0.25 + 1)
    dval = lgb.Dataset(X_val, label=y_val[:, i], reference=dtrain, weight=items['perishable'] * 0.25 + 1, categorical_feature=cate_vars)
    bst = lgb.train(params, dtrain, num_boost_round=MAX_ROUNDS, valid_sets=[dtrain, dval], early_stopping_rounds=125, verbose_eval=500)
    print('\n'.join(('%s: %.2f' % x for x in sorted(zip(X_train.columns, bst.feature_importance('gain')), key=lambda x: x[1], reverse=True))))
    val_pred.append(bst.predict(X_val, num_iteration=bst.best_iteration or MAX_ROUNDS))
    test_pred.append(bst.predict(X_test, num_iteration=bst.best_iteration or MAX_ROUNDS))