Kaggle competition: Titanic - Machine Learning from Disaster
sales.d = sales['d'].apply(lambda x: x.split('_')[1] ).astype(np.int16)
 cols = sales.dtypes.index.tolist()
types = sales.dtypes.values.tolist()
for i,type in enumerate(types):
if type.name == 'category':
sales[cols[i]] = sales[cols[i]].cat.codes<drop_column>
tra=pd.read_csv('.. /input/train.csv')
 tes=pd.read_csv('.. /input/test.csv' )
sales.drop('date',axis=1,inplace=True )<categorify>
x=tra.drop(['Name','PassengerId','Ticket','Survived'],axis=1)
 x_t=tes.drop(['Name','PassengerId','Ticket'],axis=1)
lags = [1,2,4,8,16,32]
for lag in lags:
sales['sold_lag_'+str(lag)] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'],as_index=False)['sold'].shift(lag ).astype(np.float16 )<data_type_conversions>
x.isna().sum()
sales['item_sold_avg'] = sales.groupby('item_id')['sold'].transform('mean' ).astype(np.float16)
 sales['state_sold_avg'] = sales.groupby('state_id')['sold'].transform('mean' ).astype(np.float16)
 sales['store_sold_avg'] = sales.groupby('store_id')['sold'].transform('mean' ).astype(np.float16)
 sales['cat_sold_avg'] = sales.groupby('cat_id')['sold'].transform('mean' ).astype(np.float16)
 sales['dept_sold_avg'] = sales.groupby('dept_id')['sold'].transform('mean' ).astype(np.float16)
 sales['cat_dept_sold_avg'] = sales.groupby(['cat_id','dept_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_item_sold_avg'] = sales.groupby(['store_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['cat_item_sold_avg'] = sales.groupby(['cat_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['dept_item_sold_avg'] = sales.groupby(['dept_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['dept_store_sold_avg'] = sales.groupby(['dept_id','store_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_cat_dept_sold_avg'] = sales.groupby(['store_id','cat_id','dept_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['store_cat_item_sold_avg'] = sales.groupby(['store_id','cat_id','item_id'])['sold'].transform('mean' ).astype(np.float16)
<prepare_x_and_y>
x_t.isna().sum()
id_list = sorted(list(set(sales['id'])))
 sold_avg_cols = [col for col in sales.columns if '_sold_avg' in col]
x_1 = sales.loc[sales['id'] == id_list[0]].set_index('id')[sold_avg_cols].values[0][:]
x_2 = sales.loc[sales['id'] == id_list[12]].set_index('id')[sold_avg_cols].values[0][:]
x_3 = sales.loc[sales['id'] == id_list[36]].set_index('id')[sold_avg_cols].values[0][:]
x_4 = sales.loc[sales['id'] == id_list[64]].set_index('id')[sold_avg_cols].values[0][:]
x_5 = sales.loc[sales['id'] == id_list[128]].set_index('id')[sold_avg_cols].values[0][:]
x_6 = sales.loc[sales['id'] == id_list[256]].set_index('id')[sold_avg_cols].values[0][:]
fig = make_subplots(rows=6, cols=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_1)) , y=x_1, showlegend=True,
mode='lines+markers', name="First sample",
marker=dict(color="cadetblue")) , row=1, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_2)) , y=x_2, showlegend=True,
mode='lines+markers', name="Second sample",
marker=dict(color="firebrick")) , row=2, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_3)) , y=x_3, showlegend=True,
mode='lines+markers', name="Third sample",
marker=dict(color="yellowgreen")) , row=3, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_4)) , y=x_4, showlegend=True,
mode='lines+markers', name="Fourth sample",
marker=dict(color="orangered")) , row=4, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_5)) , y=x_5, showlegend=True,
mode='lines+markers', name="Fifth sample",
marker=dict(color="mediumpurple")) , row=5, col=1)
 fig.add_trace(go.Scatter(x=np.arange(len(x_6)) , y=x_6, showlegend=True,
mode='lines+markers', name="Sixth sample",
marker=dict(color="navy")) , row=6, col=1)
 fig.update_layout(height=1500, width=900, title_text="Observing some randomly chosen '_sold_avg' trends after applying lag:")
 fig.show()<data_type_conversions>
x.Age=x.Age.fillna(x.Age.mean())
 x_t.Age=x_t.Age.fillna(x_t.Age.mean() )
sales['rolling_sold_mean'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.rolling(window=6 ).mean() ).astype(np.float16 )<data_type_conversions>
x.Cabin=x.Cabin.fillna('U')
 x_t.Cabin=x_t.Cabin.fillna('U' )
sales['expanding_sold_mean'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform(lambda x: x.expanding(2 ).mean() ).astype(np.float16 )<set_options>
x.Embarked=x.Embarked.fillna('S')
 x_t.Embarked=x_t.Embarked.fillna('S' )
gc.collect()<data_type_conversions>
x_t.Fare=x_t.Fare.fillna(x_t.Fare.mean() )
sales['daily_avg_sold'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id','d'])['sold'].transform('mean' ).astype(np.float16)
 sales['avg_sold'] = sales.groupby(['id', 'item_id', 'dept_id', 'cat_id', 'store_id', 'state_id'])['sold'].transform('mean' ).astype(np.float16)
 sales['selling_trend'] =(sales['daily_avg_sold'] - sales['avg_sold'] ).astype(np.float16)
 sales.drop(['daily_avg_sold','avg_sold'],axis=1,inplace=True )<filter>
x.Cabin = x.Cabin.map(lambda z: z[0])
 x_t.Cabin = x_t.Cabin.map(lambda z: z[0] )
sales = sales[sales['d']>=32]<set_options>
gc.collect()<load_pretrained>
x= pd.get_dummies(x)
 x_t=pd.get_dummies(x_t )
sales.to_pickle('salesdata.pkl')
 del sales<set_options>
x=x.drop(['Cabin_T'],axis=1 )
gc.collect()<categorify>
y=tra['Survived']
data = pd.read_pickle('salesdata.pkl')
 validation = data[(data['d']>=1914)&(data['d']<1942)][['id','d','sold']]
test = data[data['d']>=1942][['id','d','sold']]
eval_prediction = test['sold']
validation_prediction = validation['sold']<set_options>
x_train,x_val,y_train,y_val=train_test_split(x,y,test_size=0.2,random_state=0 )
gc.collect()<set_options>
reg=RandomForestClassifier(n_estimators=100000,random_state=0)
 reg.fit(x_train,y_train )
gc.collect()<prepare_x_and_y>
X_train, y_train = df[df['d']<1914].drop('sold',axis=1), df[df['d']<1914]['sold']
X_valid, y_valid = df[(df['d']>=1914)&(df['d']<1942)].drop('sold',axis=1), df[(df['d']>=1914)&(df['d']<1942)]['sold']
X_test = df[df['d']>=1942].drop('sold',axis=1 )<set_options>
y_pred=reg.predict(x_val )
gc.collect()<define_search_space>
accuracy_score(y_val, y_pred )
%%time
valgrid = {'n_estimators':hp.quniform('n_estimators', 900, 1500, 100),
'learning_rate':hp.quniform('learning_rate', 0.01, 0.4, 0.01),
'max_depth':hp.quniform('max_depth', 3,10,1),
'num_leaves':hp.quniform('num_leaves', 25,100,25),
'subsample':hp.quniform('subsample', 0.5, 0.9, 0.1),
'colsample_bytree':hp.quniform('colsample_bytree', 0.5, 0.9, 0.1),
'min_child_weight':hp.quniform('min_child_weight', 100, 500, 100)
 }
def objective(params):
params = {'n_estimators': int(params['n_estimators']),
'learning_rate': params['learning_rate'],
'max_depth': int(params['max_depth']),
'num_leaves': int(params['num_leaves']),
'subsample': params['subsample'],
'colsample_bytree': params['colsample_bytree'],
'min_child_weight': params['min_child_weight']}
lgb_a = lgb(**params)
 score = cross_val_score(lgb_a, X_train, y_train, cv=2, n_jobs=-1 ).mean()
return score
bestP = fmin(fn= objective, space= valgrid, max_evals=20, rstate=np.random.RandomState(123), algo=tpe.suggest )<set_options>
pred=reg.predict(x_t)
 new_pred=pred.astype(int)
 output=pd.DataFrame({'PassengerId':tes['PassengerId'],'Survived':new_pred})
 output.to_csv('Titanic.csv', index=False )