Kaggle competition: Santander Customer Transaction Prediction
all_data["Functional"] = all_data["Functional"].fillna("Typ" )<data_type_conversions>
train = pd.read_csv(r".. /input/train.csv")
 test = pd.read_csv(r".. /input/test.csv" )
all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0] )<data_type_conversions>
cols = train.columns.values.tolist() [2: ]
predictors = train[cols]
target = train[['target']]
pre_test = test[cols]
all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0] )<categorify>
%%time
sfl = StratifiedKFold(n_splits = 3, shuffle=True)
 pred_test_y = np.zeros(( test.shape[0]))
seed = 2019
N = 0
for train_indices, test_indices in sfl.split(predictors, target):
params = {
'num_leaves': 15,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': seed,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
X_train, X_test = predictors.iloc[train_indices], predictors.iloc[test_indices]
y_train, y_test = target.iloc[train_indices], target.iloc[test_indices]
lgtrain = lgb.Dataset(X_train, label=y_train)
 lgval = lgb.Dataset(X_test, label=y_test)
 evals_result = {}
model2 = lgb.train(params, lgtrain, 10000, valid_sets=[lgval], early_stopping_rounds=100, verbose_eval=20,
evals_result=evals_result)
 pred_val = model2.predict(X_test, num_iteration=model2.best_iteration)
 pred_test_y += model2.predict(pre_test, num_iteration=model2.best_iteration)
 print("Validation score is :", roc_auc_score(y_test, pred_val))
print(N, "Iteration completed")
 seed+= 2000
N+=1
pred_test = pred_test_y/N
all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0] )<data_type_conversions>
important_cols= ['var_33','var_6','var_91','var_170','var_13','var_1','var_190','var_9','var_21','var_127','var_108','var_174','var_173','var_18',
'var_146','var_34','var_110','var_92','var_78','var_198','var_22','var_169','var_165','var_121','var_133','var_184','var_12',
'var_94','var_75','var_99','var_53','var_154','var_76','var_80','var_191','var_166','var_157','var_122','var_36','var_26',
'var_109','var_131','var_107','var_164','var_2','var_115','var_40','var_179','var_130','var_192','var_81','var_32','var_177',
'var_147','var_67','var_56','var_123','var_141','var_44','var_197','var_172','var_89','var_118','var_128','var_162','var_149',
'var_180', 'var_71', 'var_139', 'var_186']
new_preds = train[important_cols]
all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0] )<data_type_conversions>
%%time
sfl = StratifiedKFold(n_splits = 3, shuffle=True)
 pred_test_y2 = np.zeros(( test.shape[0]))
seed = 2019
N = 0
for train_indices, test_indices in sfl.split(predictors, target):
params = {
'num_leaves': 15,
'max_bin': 119,
'min_data_in_leaf': 11,
'learning_rate': 0.02,
'min_sum_hessian_in_leaf': 0.00245,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': 0.05,
'lambda_l1': 4.972,
'lambda_l2': 2.276,
'min_gain_to_split': 0.65,
'max_depth': 14,
'save_binary': True,
'seed': seed,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
X_train, X_test = predictors.iloc[train_indices], predictors.iloc[test_indices]
y_train, y_test = target.iloc[train_indices], target.iloc[test_indices]
lgtrain = lgb.Dataset(X_train, label=y_train)
 lgval = lgb.Dataset(X_test, label=y_test)
 evals_result = {}
model3 = lgb.train(params, lgtrain, 10000, valid_sets=[lgval], early_stopping_rounds=100, verbose_eval=20,
evals_result=evals_result)
 pred_val = model3.predict(X_test, num_iteration=model3.best_iteration)
 pred_test_y2 += model2.predict(pre_test, num_iteration=model2.best_iteration)
 print("Validation score is :", roc_auc_score(y_test, pred_val))
print(N, "Iteration completed")
 seed+= 2000
N+=1
pred_test2 = pred_test_y2/N
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None")
<create_dataframe>
predictions = pred_test*0.5 + pred_test2*0.5
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head()<data_type_conversions>
predictions = pd.DataFrame(predictions, columns =['target'])
 sub = pd.concat([test[['ID_code']], predictions[['target']]], axis = 1)
 sub.to_csv('submission.csv', index=False )