Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<define_variables>
sns.set_style('whitegrid')
nfold = 5<set_options>
train_df = pd.read_csv('.. /input/train.csv')
 print('Rows: ',train_df.shape[0],'Columns: ',train_df.shape[1])
 train_df.info()
gc.collect()<choose_model_class>
train_df['target'].value_counts()
skf = StratifiedKFold(n_splits=nfold, shuffle=True, random_state=2019 )<split>
test_df = pd.read_csv('.. /input/test.csv' )
oof = np.zeros(len(train_df))
predictions = np.zeros(( len(test_df),nfold))
i = 1
for train_index, valid_index in skf.split(train_df, target.values):
print("
fold {}".format(i))
xg_train = lgb.Dataset(train_df.iloc[train_index][predictors].values,
label=target.iloc[train_index].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train_df.iloc[valid_index][predictors].values,
label=target.iloc[valid_index].values,
feature_name=predictors,
free_raw_data = False 
)
 clf = lgb.train(param_lgb, xg_train, 10000, valid_sets = [xg_valid], verbose_eval=1000)
 oof[valid_index] = clf.predict(train_df.iloc[valid_index][predictors].values, num_iteration=clf.best_iteration)
 predictions[:,i-1] += clf.predict(test_df[predictors], num_iteration=clf.best_iteration)
 i = i + 1
print("
CV AUC: {:<0.6f}".format(metrics.roc_auc_score(target.values, oof)) )<feature_engineering>
X_test = test_df.drop('ID_code',axis=1 )
print("Rank averaging on", nfold, "fold predictions")
 rank_predictions = np.zeros(( predictions.shape[0],1))
for i in range(nfold):
rank_predictions[:, 0] = np.add(rank_predictions[:, 0], rankdata(predictions[:, i].reshape(-1,1)) /rank_predictions.shape[0])
 rank_predictions /= nfold<create_dataframe>
X = train_df.drop(['ID_code','target'],axis=1)
 y = train_df['target']
sub_df = pd.DataFrame({"ID_code": ID_code.values})
 sub_df["target"] = rank_predictions
sub_df[:10]<save_to_csv>
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )
sub_df.to_csv("Customer_Transaction_rank_predictions.csv", index=False )<set_options>
params = {'num_leaves': 8,
'min_data_in_leaf': 42,
'objective': 'binary',
'max_depth': 16,
'learning_rate': 0.0123,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.8,
'feature_fraction': 0.8201,
'bagging_seed': 11,
'reg_alpha': 1.728910519108444,
'reg_lambda': 4.9847051755586085,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 4}
%matplotlib inline<load_from_csv>
prediction = np.zeros(len(X_test))
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)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/5
Kaggle=1
if Kaggle==0:
train=pd.read_csv("train.csv")
 test=pd.read_csv("test.csv")
 sample_sub=pd.read_csv("sample_submission.csv")
 else:
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv")
 sample_sub=pd.read_csv(".. /input/sample_submission.csv" )<count_values>
train_pool = Pool(X,y)
 m = CatBoostClassifier(iterations=300,eval_metric="AUC", boosting_type = 'Ordered')
 m.fit(X,y,silent=True)
 y_pred1 = m.predict(X_test)
 m.best_score_
train['target'].value_counts()<import_modules>
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )
from sklearn.model_selection import train_test_split,KFold, cross_val_score, GridSearchCV,StratifiedKFold,StratifiedShuffleSplit
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, roc_auc_score
import eli5
from eli5.sklearn import PermutationImportance
from sklearn.preprocessing import StandardScaler
import xgboost as xgb
import lightgbm as lgb
from tqdm import tqdm_notebook
import feather
from bayes_opt import BayesianOptimization<drop_column>
sub1 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub1["target"] = y_pred1
sub1.to_csv("submission1.csv", index=False )
<prepare_x_and_y><EOS> 
sub2 = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub2["target"] =(prediction + y_pred1)/2
sub2.to_csv("submission2.csv", index=False )