Kaggle competition: Santander Customer Transaction Prediction
x1 = train_set[train_set['date_block_num']==6].reset_index().iloc[for_val]
for i in range(7,33):
x = train_set[train_set['date_block_num']==i].reset_index().iloc[for_val]
x1 = x1.append(x)
 val_data = x1.copy()<categorify>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
 submit_data = pd.read_csv('.. /input/sample_submission.csv')
 print("Data Loaded" )
x2 = train_set[train_set['date_block_num']==6].reset_index().iloc[for_fit]
for i in range(7,33):
xx = train_set[train_set['date_block_num']==i].reset_index().iloc[for_fit]
x2 = x2.append(xx)
 fit_data = x2.copy()<prepare_x_and_y>
target = train_data['target']
tar = train_data['target']
features = [c for c in train_data.columns if c not in ['ID_code', 'target']]
print("Data is ready!" )
%%time
drop_cols = ['date_block_num', 'shop_id', 'item_id', 'item_price_median', 'year', 'month', 'item_cnt_next_month','item_category_id', 'index']
X_train = fit_data.drop(drop_cols, axis=1)
 Y_train = fit_data['item_cnt_next_month']
X_val = val_data.drop(drop_cols, axis=1)
 Y_val = val_data['item_cnt_next_month']<train_model>
train_data = train_data.drop(["ID_code"], axis=1)
 test_data = test_data.drop(["ID_code"], axis=1 )
def LGBReg(x_train, y_train, x_val, y_val):
lgb_reg = LGBMRegressor(
 n_jobs=-1,
tree_method='auto',
learning_rate=0.02,
max_depth=8,
n_estimators=1000,
colsample_bytree=0.8,
subsample=0.8,
seed=42)
 lgb_reg.fit(
 x_train,
y_train,
eval_metric="rmse",
eval_set=[(x_train, y_train),(x_val, y_val)],
verbose=10,
early_stopping_rounds = 10)
 return lgb_reg<compute_test_metric>
target0 = train_data[target==0]
target1 = train_data[target==1]
%%time
lgb_reg_1 = LGBReg(X_train, Y_train, X_val, Y_val )<predict_on_test>
for i in range(0,199):
ch = 'var_' + str(i)
 target0[ch] = np.random.permutation(target0[ch])
 for i in range(0,199):
ch = 'var_' + str(i)
 target1[ch] = np.random.permutation(target1[ch] )
lgb_test_pred = lgb_reg_1.predict(X_test ).clip(0, 20 )<save_to_csv>
target0 = target0.append(target1)
 train_data = train_data.append(target0)
 train_data = train_data.sample(frac=1)
 target = train_data['target']
submission37 = pd.DataFrame(test['ID'])
 submission37['item_cnt_month'] = lgb_test_pred
submission37.to_csv('submission37.csv', index=False )<save_to_csv>
train_data = train_data.drop(["target"], axis=1 )
submission38 = pd.DataFrame(test['ID'])
 submission38['item_cnt_month'] = lgb_test_pred.round()
submission38.to_csv('submission38.csv', index=False )<import_modules>
train_size = train_data.shape[0]
test_size = test_data.shape[0]
from statsmodels.regression.linear_model import OLS
import statsmodels.api as sm<data_type_conversions>
train_test = train_data
train_test = train_test.append(test_data )
x = sm.add_constant(X_train.fillna(0))<train_model>
%%time
train_test = pd.DataFrame(scale(train_test.values), columns=train_test.columns, index=train_test.index )
model2 = OLS(
 Y_train,
x
).fit()
print(model2.summary() )<feature_engineering>
kmeans = KMeans(n_clusters=K)
 kmeans.fit(train_test)
 train_test['k_labels'] = kmeans.labels_
X_test['const'] = 1<predict_on_test>
temp_df = train_test['k_labels']
train_test['weight1'] = temp_df.apply(lambda x: x*3)
 train_test['weight2'] = temp_df.apply(lambda x: x*7)
 train_test['weight3'] = temp_df.apply(lambda x: x*11)
 train_test['weight4'] = temp_df.apply(lambda x: x*5)
 train_test['weight5'] = temp_df.apply(lambda x: x*13)
 train_test['weight6'] = temp_df.apply(lambda x: x*17)
 train_test['weight7'] = temp_df.apply(lambda x: x*19)
 train_test['weight8'] = temp_df.apply(lambda x: x*23)
 train_test['weight9'] = temp_df.apply(lambda x: x*29)
 train_test['weight10'] = temp_df.apply(lambda x: x*31)
 train_test['weight11'] = temp_df.apply(lambda x: x*37)
 train_test['weight12'] = temp_df.apply(lambda x: x*41)
 train_test['weight13'] = temp_df.apply(lambda x: x*43)
 train_test['weight14'] = temp_df.apply(lambda x: x*47)
 train_test['weight15'] = temp_df.apply(lambda x: x*53)
 train_test['weight16'] = temp_df.apply(lambda x: x*59)
 train_test['weight17'] = temp_df.apply(lambda x: x*61)
 train_test['weight18'] = temp_df.apply(lambda x: x*67)
 train_test['weight19'] = temp_df.apply(lambda x: x*71)
 train_test['weight20'] = temp_df.apply(lambda x: x*73)
 train_test.head(2 )
y_predict_OLS = model2.predict(X_test ).clip(0, 20 )<save_to_csv>
cluster_count = pd.Series()
for i in range(0,K):
print(i , " " , train_test.loc[train_test['k_labels']==i].shape[0])
 temp =(train_test.loc[train_test['k_labels']==i].shape[0])
 cluster_count = cluster_count.append(pd.Series(temp))
clusterCountVal = pd.DataFrame()
clusterCountVal = cluster_count.to_frame()
submission39 = pd.DataFrame(test['ID'])
 submission39['item_cnt_month'] = y_predict_OLS
submission39.to_csv('submission39.csv', index=False )<compute_test_metric>
train_data = train_test[:test_size]
test_data = train_test[train_size:]
rmse_train_1 = sqrt(mean_squared_error(Y_train, model2.predict(x)))
 rmse_train_1<compute_test_metric>
params = {}
params['bagging_freq'] = 5
params['bagging_fraction'] = 0.0331
params['random_state'] = 42
params['learning_rate'] = 0.0123
params['boost_from_average'] = False
params['boosting_type'] = 'gbdt'
params['feature_fraction'] = 0.045
params['objective'] = 'binary'
params['metric'] = 'auc'
params['min_data_in_leaf'] = 80
params['num_leaves'] = 13
params['num_threads'] = 8
params['tree_learner'] = 'serial'
params['max_depth'] = -1
params['min_sum_hessian_in_leaf'] = 10.0
params['verbosity'] = 1
params['bagging_seed'] = 42
params['seed'] = 42
rmse_val_1 = sqrt(mean_squared_error(Y_val,model2.predict(sm.add_constant(X_val.fillna(0)))))
 rmse_val_1<import_modules>
num_folds = 10
features = [c for c in test_data.columns if c not in ['ID_code', 'target']]
folds = StratifiedKFold(n_splits=num_folds,shuffle=True, random_state=42)
 oof = np.zeros(len(train_data))
getVal = np.zeros(len(train_data))
predictions = np.zeros(len(tar))
print(predictions.shape)
 feature_importance_df = pd.DataFrame()
import numpy as np
import pandas as pd
import pickle
import time
import sys
import gc
import os
from sklearn.metrics import mean_squared_error
from xgboost import XGBRegressor<define_variables>
%%time
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_data.values, target.values)) :
X_train, y_train = train_data.iloc[trn_idx][features], target.iloc[trn_idx]
X_valid, y_valid = train_data.iloc[val_idx][features], target.iloc[val_idx]
X_tr, y_tr = X_train.values, y_train.values
X_tr = pd.DataFrame(X_tr)
 print("Fold idx:{}".format(fold_ + 1))
trn_data = lgb.Dataset(X_tr, label=y_tr)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(params, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
 oof[val_idx] = clf.predict(train_data.iloc[val_idx][features], num_iteration=clf.best_iteration)
 getVal[val_idx]+= clf.predict(train_data.iloc[val_idx][features], num_iteration=clf.best_iteration)/ folds.n_splits
predictions += clf.predict(test_data[features], num_iteration=clf.best_iteration)/ folds.n_splits
<load_pretrained><EOS> 
submit_data['target'] = pd.DataFrame(predictions)
 submit_data.to_csv("LGBMwithClustering.csv", index=False )