Kaggle competition: Santander Customer Transaction Prediction
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df.pop('target')
 train_df.drop('ID_code',axis=1,inplace=True)
 test_df.drop('ID_code',axis=1,inplace=True )<init_hyperparams>
train=pd.read_csv(".. /input/train.csv",index_col="ID_code")
 test=pd.read_csv(".. /input/test.csv",index_col="ID_code")
 target=train.target
train=train.drop("target",axis=1 )
param = {
'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1}<concatenate>
from sklearn.ensemble import AdaBoostClassifier
from sklearn import metrics
from sklearn.model_selection import KFold
import time
test_df['target'] = -1
train_df['i_am_train'] = 1
test_df['i_am_train'] = 0
full_df = pd.concat([train_df, test_df], axis=0 )<count_values>
import lightgbm as lgb
for f in features:
full_df[f+'_counts'] = full_df[f].map(full_df[f].value_counts().to_dict() )<drop_column>
train_df = full_df.loc[full_df['i_am_train']==1]
test_df = full_df.loc[full_df['i_am_train']==0]
del train_df['i_am_train'], test_df['i_am_train'], test_df['target'], full_df<define_variables>
def train_model(params,train=train,target=target,test=test,samples=-1):
kford=KFold(n_splits=5,random_state=2,shuffle=True)
 start_time=time.time()
aucs=[]
test_preds=[]
if samples<=-1:
samples=train.shape[0]
else:
samples=min(train.shape[0],samples)
 print("
print("
print("fit on {} samples".format(samples))
for ford,(train_idx,val_idx)in enumerate(kford.split(train[:samples],target[:samples])) :
print("
print("
sample_x=train.iloc[train_idx].values
sample_y=target.iloc[train_idx].values
sample_val_x=train.iloc[val_idx].values
sample_val_y=target.iloc[val_idx].values
train_dataset=lgb.Dataset(data=sample_x,label=sample_y)
 val_dataset=lgb.Dataset(data=sample_val_x,label=sample_val_y)
 ford_time=time.time()
clf=lgb.train(params,train_dataset,valid_sets=[train_dataset,val_dataset],verbose_eval=50)
 print("epoch cost time {:1}s".format(time.time() -ford_time))
y_pred_prob=clf.predict(sample_x,clf.best_iteration)
 y_val_pred_prob=clf.predict(sample_val_x,clf.best_iteration)
 train_auc=metrics.roc_auc_score(sample_y,y_pred_prob)
 val_auc=metrics.roc_auc_score(sample_val_y,y_val_pred_prob)
 print("train auc:{:4},val auc:{:4}".format(train_auc,val_auc))
aucs.append([train_auc,val_auc])
 test_preds.append(clf.predict(test,clf.best_iteration))
end_time=time.time()
val_aucs=[auc[1] for auc in aucs]
print("using {} samples,total time:{:1}s,mean val auc:{:4}".format(samples,end_time-start_time,np.mean(val_aucs)))
 test_preds=pd.DataFrame(test_preds ).T
test_preds.index=test.index
return test_preds
num_folds = 5
folds = StratifiedKFold(n_splits=num_folds, shuffle=True, random_state=42)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))<define_variables>
params={
"metric":"auc",
"num_threads":8,
"object":"binary",
"boosting":"gbdt",
"num_iterations":10000,
"learning_rate":0.03,
"num_leaves":32,
"max_depth":2,
"early_stopping_round":100,
"lambda_l2":1,
"min_data_in_leaf":2000,
"bagging_fraction":0.9,
"feature_fraction":0.9
}
test_preds=train_model(params,samples=-1 )
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]<split>
submission=pd.DataFrame(test_preds.mean(axis=1),columns=["target"])
 submission.head()
submission.to_csv("submission.csv" )