text_html_output_1.png text_plain_output_1.png
from ml_metrics import rmsle

from sklearn.cross_validation import train_test_split

import math

import numpy as np

import xgboost as xgb

def evalerror(preds, dtrain):
    labels = dtrain.get_label()
    assert len(preds) == len(labels)
    labels = labels.tolist()
    preds = preds.tolist()
    terms_to_sum = [(math.log(labels[i] + 1) - math.log(max(0, preds[i]) + 1)) ** 2.0 for i, pred in enumerate(labels)]
    return ('error', (sum(terms_to_sum) * (1.0 / len(preds))) ** 0.5)

test.shape

train = train[train['Semana'] > 8]

ids = test['id']
test = test.drop(['id'], axis=1)
y = train['Demanda_uni_equil']
X = train[test.columns.values]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1729)
del train
del X
del y

params = {}
params['objective'] = 'reg:linear'
params['eta'] = 0.1
params['max_depth'] = 5
params['subsample'] = 0.8
params['colsample_bytree'] = 0.6
params['silent'] = True
params['booster'] = 'gbtree'
test_preds = np.zeros(test.shape[0])
xg_train = xgb.DMatrix(X_train, label=y_train)
del X_train
del y_train
xg_test = xgb.DMatrix(X_test)
del X_test
watchlist = [(xg_train, 'train')]

preds = xgclassifier.predict(xg_test, ntree_limit=xgclassifier.best_iteration)

print('RMSLE Score:', rmsle(y_test, preds))
del preds
del y_test