In [2]:
import pandas as pd
import numpy as np
import os
import math
import graphlab
import graphlab as gl
import graphlab.aggregate as agg
from graphlab import SArray
In [3]:
'''钢炮'''
path = '/home/zongyi/bimbo_data/'
In [4]:
train = gl.SFrame.read_csv(path + 'train_lag5.csv', verbose=False)
In [5]:
town = gl.SFrame.read_csv(path + 'towns.csv', verbose=False)
train = train.join(town, on=['Agencia_ID','Producto_ID'], how='left')
train = train.fillna('t_c',1)
train = train.fillna('tcc',0)
train = train.fillna('tp_sum',0)
del train['Town']
In [6]:
del train['id']
del train['Venta_uni_hoy']
del train['Venta_hoy']
del train['Dev_uni_proxima']
del train['Dev_proxima']
del train['Demanda_uni_equil']
In [7]:
# relag_train = gl.SFrame.read_csv(path + 're_lag_train.csv', verbose=False)
# train = train.join(relag_train, on=['Cliente_ID','Producto_ID','Semana'], how='left')
# train = train.fillna('re_lag1',0)
# train = train.fillna('re_lag2',0)
# train = train.fillna('re_lag3',0)
# train = train.fillna('re_lag4',0)
# train = train.fillna('re_lag5',0)
# del relag_train
In [8]:
# pd = gl.SFrame.read_csv(path + 'products.csv', verbose=False)
# train = train.join(pd, on=['Producto_ID'], how='left')
# train = train.fillna('prom',0)
# train = train.fillna('weight',0)
# train = train.fillna('pieces',1)
# train = train.fillna('w_per_piece',0)
# train = train.fillna('healthy',0)
# train = train.fillna('drink',0)
# del train['brand']
# del train['NombreProducto']
# del pd
In [9]:
# client = gl.SFrame.read_csv(path + 'clients.csv', verbose=False)
# train = train.join(client, on=['Cliente_ID'], how='left')
# del client
In [10]:
# cluster = gl.SFrame.read_csv(path + 'prod_cluster.csv', verbose=False)
# cluster = cluster[['Producto_ID','cluster']]
# train = train.join(cluster, on=['Producto_ID'], how='left')
In [44]:
train
Out[44]:
In [12]:
# Make a train-test split
train_data, test_data = train.random_split(0.999)
# Create a model.
model = gl.boosted_trees_regression.create(train_data, target='Demada_log',
step_size=0.1,
max_iterations=500,
max_depth = 10,
metric='rmse',
random_seed=395,
column_subsample=0.7,
row_subsample=0.85,
validation_set=test_data,
model_checkpoint_path=path,
model_checkpoint_interval=500)
In [71]:
model1 = gl.boosted_trees_regression.create(train, target='Demada_log',
step_size=0.1,
max_iterations=4,
max_depth = 10,
metric='rmse',
random_seed=395,
column_subsample=0.7,
row_subsample=0.85,
validation_set=None,
resume_from_checkpoint=path+'model_checkpoint_4',
model_checkpoint_path=path,
model_checkpoint_interval=2)
In [13]:
model
Out[13]:
In [15]:
w = model.get_feature_importance()
In [16]:
w = w.add_row_number()
In [17]:
w
Out[17]:
In [21]:
from IPython.core.pylabtools import figsize
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
sns.set_style('darkgrid', {'grid.color': '.8','grid.linestyle': u'--'})
%matplotlib inline
figsize(12, 6)
plt.bar(w['id'], w['count'], tick_label=w['name'])
plt.xticks(rotation=45)
Out[21]:
In [20]:
# Save predictions to an SArray
predictions = model.predict(train)
# Evaluate the model and save the results into a dictionary
results = model.evaluate(train)
print results
In [36]:
model.summary()
In [ ]:
In [23]:
test = gl.SFrame.read_csv(path + 'test_lag5.csv', verbose=False)
test = test.join(town, on=['Agencia_ID','Producto_ID'], how='left')
del test['Town']
test = test.fillna('t_c',1)
test = test.fillna('tcc',0)
test = test.fillna('tp_sum',0)
In [24]:
test
Out[24]:
In [25]:
ids = test['id']
In [26]:
del test['id']
In [27]:
demand_log = model.predict(test)
In [30]:
sub = gl.SFrame({'id':ids,'Demanda_uni_equil':demand_log})
In [33]:
import math
sub['Demanda_uni_equil'] = sub['Demanda_uni_equil'].apply(lambda x: math.expm1(max(0, x)))
In [34]:
sub
Out[34]:
In [35]:
sub.save(path+'gbrt_sub3.csv',format='csv')
In [43]:
math.expm1(math.log1p(2))
Out[43]:
In [40]:
Out[40]:
In [ ]: