Kaggle competition: Santander Customer Transaction Prediction
X_train.to_csv("X_train.csv", index=False)
 X_valid.to_csv("X_valid.csv", index=False)
 X_test.to_csv("X_test.csv", index=False)
 y_train.to_csv("y_train.csv", index=False)
 y_valid.to_csv("y_valid.csv", index=False )<train_model>
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
import matplotlib.pyplot as plt
import lightgbm as lgb
import seaborn as sns
import warnings
import time
%%time
xgb_model = XGBRegressor(max_depth=10,
n_estimators=1000,
min_child_weight=1000,
colsample_bytree=0.7,
subsample=0.7,
eta=0.3,
seed=0)
 xgb_model.fit(X_train,
y_train,
eval_metric="rmse",
eval_set=[(X_train, y_train),(X_valid, y_valid)],
verbose=20,
early_stopping_rounds=10)
<compute_test_metric>
fold_n=5
folds = StratifiedKFold(n_splits=fold_n, shuffle=True, random_state=10)
 %matplotlib inline
%precision 4
warnings.filterwarnings('ignore')
 plt.style.use('ggplot')
 np.set_printoptions(suppress=True)
 pd.set_option("display.precision", 15 )
%%time
xgb_train_pred = xgb_model.predict(X_train)
 xgb_val_pred = xgb_model.predict(X_valid)
 xgb_test_pred = xgb_model.predict(X_test)
 print('Train rmse:', np.sqrt(mean_squared_error(y_train, xgb_train_pred)))
 print('Validation rmse:', np.sqrt(mean_squared_error(y_valid, xgb_val_pred)) )<drop_column>
train= pd.read_csv(".. /input/train.csv")
 test = pd.read_csv('.. /input/test.csv' )
xgb_model.save_model("xgboost")
 del xgb_model, xgb_train_pred
gc.collect()<save_to_csv>
sample_submission = pd.read_csv('.. /input/sample_submission.csv')
 sample_submission.head()
submission = pd.DataFrame(test_df['ID'], columns=['ID'])
 submission['item_cnt_month'] = xgb_test_pred.clip(0., 20.)
submission.to_csv('submission_xgb.csv', index=False)
 submission.head(10 )<train_model>
train.isnull().values.any()
%%time
model=LGBMRegressor(
 n_estimators=200,
learning_rate=0.03,
num_leaves=42,
colsample_bytree=0.95,
subsample=0.8,
max_depth=9,
reg_alpha=0.03,
reg_lambda=0.07,
min_split_gain=0.02,
min_child_weight=40,
silent=False)
 model.fit(X_train, y_train )<compute_test_metric>
test.isnull().values.any()
%%time
lgb_train_pred = model.predict(X_train)
 lgb_val_pred = model.predict(X_valid)
 lgb_test_pred = model.predict(X_test)
 print('Train rmse:', np.sqrt(mean_squared_error(y_train, lgb_train_pred)))
 print('Validation rmse:', np.sqrt(mean_squared_error(y_valid, lgb_val_pred)))
<drop_column>
cols=["target","ID_code"]
X = train.drop(cols,axis=1)
 y = train["target"]
model.booster_.save_model('lgb_classifier.txt')
 del model, lgb_train_pred
gc.collect()<prepare_output>
X_test = test.drop("ID_code",axis=1 )
final_test_preds = 0.5 * lgb_test_pred + 0.5 * xgb_test_pred
final_test_preds = final_test_preds.clip(0., 20.)<save_to_csv>
params = {'objective' : "binary",
'boost':"gbdt",
'metric':"auc",
'boost_from_average':"false",
'num_threads':8,
'learning_rate' : 0.01,
'num_leaves' : 13,
'max_depth':-1,
'tree_learner' : "serial",
'feature_fraction' : 0.05,
'bagging_freq' : 5,
'bagging_fraction' : 0.4,
'min_data_in_leaf' : 80,
'min_sum_hessian_in_leaf' : 10.0,
'verbosity' : 1}
submission = pd.DataFrame(test_df['ID'], columns=['ID'])
 submission['item_cnt_month'] = final_test_preds
submission.to_csv('submission.csv', index=False)
 submission.head(10 )<create_dataframe>
%%time
y_pred_lgb = np.zeros(len(X_test))
num_round = 1000000
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 lgb_model = lgb.train(params,train_data,num_round,
valid_sets = [train_data, valid_data],verbose_eval=1000,early_stopping_rounds = 3500)
 y_pred_lgb += lgb_model.predict(X_test, num_iteration=lgb_model.best_iteration)/5
<train_model><EOS> 
submission_lgb = pd.DataFrame({
"ID_code": test["ID_code"],
"target": y_pred_lgb
})
 submission_lgb.to_csv('submission_lgb.csv', index=False )