Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<choose_model_class>
%matplotlib inline
sns.set()
print(os.listdir(".. /input"))
warnings.filterwarnings("ignore" )
x = Dense(512, activation='relu' )(base_model.layers[-2].output)
 x = Dropout(0.35 )(x)
 out = Dense(5, activation = 'softmax' )(x)
 model = Model(inputs = input_layer, outputs = out )<choose_model_class>
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv('.. /input/test.csv')
 train.head()
optimizer = RAdam(lr=0.0005)
 es = EarlyStopping(monitor='cohens_kappa', mode='auto', verbose=1, patience=3,restore_best_weights=True)
 rlrop = ReduceLROnPlateau(monitor='cohens_kappa',
factor=0.2,
patience=5,
verbose=1,
mode='auto',
min_lr=1e-6)
 callback_list = [ rlrop ]
model.compile(optimizer = optimizer, loss = "categorical_crossentropy", metrics = ["accuracy",cohens_kappa] )<feature_engineering>
print(train.isnull().values.any())
 print(test.isnull().values.any() )
K.get_session().run(tf.local_variables_initializer() )<train_model>
es = ft.EntitySet(id='Santander')
 es.entity_from_dataframe(dataframe=train[features],
entity_id='train',
make_index = True,
index='index')
 fm, feat= ft.dfs(entityset=es,
target_entity='train',
trans_primitives=['multiply_numeric','add_numeric'],
max_depth=1 )
history = model.fit_generator(generator = train_generator,
steps_per_epoch = len(train_generator),
epochs = 17,
validation_data = validation_generator,
validation_steps = len(validation_generator),
callbacks = callback_list )<predict_on_test>
train=pd.concat(( train,fm.iloc[:,len(features):]),axis=1)
 del fm
gc.collect()
fm=pd.DataFrame()
train.info()
predprobs = model.predict_generator(test_generator, steps=len(test_generator))<drop_column>
es.entity_from_dataframe(dataframe=test[features],
entity_id='test',
make_index = True,
index='index')
 fm_test, feat= ft.dfs(entityset=es,
target_entity='test',
trans_primitives=['multiply_numeric','add_numeric'],
max_depth=1 )
shutil.rmtree('train_data_cropped')
 shutil.rmtree('test_dir_processed' )<prepare_output>
test=pd.concat(( test,fm_test.iloc[:,len(features):]),axis=1)
 del fm_test
gc.collect()
fm_test=pd.DataFrame()
test.info()
predictions = []
for i in predprobs:
predictions.append(np.argmax(i))<prepare_output>
random_state = 222
kfold = StratifiedKFold(n_splits=12,shuffle=True,random_state=random_state)
 pred_val = np.zeros(len(train))
pred_test_baseline = np.zeros(len(test))
feature_base=train.columns.tolist() [2:202]
test_df_orig["diagnosis"] = predictions<feature_engineering>
param = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'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
}
test_ids = test_df_orig["id_code"].values.tolist()
for i in range(len(test_ids)) :
imgname = test_ids[i]
newname = imgname.split('.')[0]
test_ids[i] = newname
test_df_orig["id_code"] = test_ids<save_to_csv>
X_train=train.iloc[:,2:]
y_train=train['target']
X_test=test.iloc[:,1:]
test_df_orig.to_csv('submission.csv',index=False )<load_from_csv>
for foldIdx,(trn_idx, val_idx)in enumerate(kfold.split(X_train.loc[:,feature_base], y_train)) :
print("Fold {}".format(foldIdx))
lgbm_base=lgb.LGBMClassifier(n_estimators=100000,random_state=random_state,**param)
 lgbm_base.fit(X_train.iloc[trn_idx][feature_base],y_train[trn_idx],
eval_set=[(X_train.iloc[trn_idx][feature_base],y_train[trn_idx]),(X_train.iloc[val_idx][feature_base],y_train[val_idx])],
early_stopping_rounds = 3000,
verbose=5000)
 pred_val[val_idx] = lgbm_base.predict_proba(X_train.loc[val_idx,feature_base], num_iteration=lgbm_base.best_iteration_)[:,1]
pred_test_baseline += lgbm_base.predict_proba(X_test[feature_base],num_iteration=lgbm_base.best_iteration_)[:,1] / kfold.n_splits
subfile = pd.read_csv('submission.csv')
 subfile.head(3 )<import_modules>
print('AUC score: %.5f' % roc_auc_score(train['target'],pred_val))
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time as t
import cv2
import warnings
import shutil
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.utils import shuffle
from keras.applications.resnet50 import ResNet50
from keras.applications import DenseNet121
from keras.utils import Sequence
from keras.layers import Dense, Dropout, Flatten, Input, ZeroPadding2D, GlobalAveragePooling2D
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras import regularizers
from keras.models import Model
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
from keras import optimizers
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras import backend as K
import tensorflow as tf<define_variables>
to_drop = [column for column in upper.columns if any(upper[column] > 0.7)]
PATH = '.. /input/'
PATH_APTOS = PATH + 'aptos2019-blindness-detection/'
PATH_2015 = PATH + 'retinopathy-train-2015/rescaled_train_896/'
PATH_KERAS = PATH + 'keras-pretrained-models/'
densenet_weights_path = PATH + 'densenet121full/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5'
PATH_AUGM = '/data_augm/'
HEIGHT, WIDTH = 256, 256
RANDOM_STATE = 974
VERBOSE = True
BATCH_SIZE = 32
DATA_AUGM = True
DATA_AUGM_FACTOR = 1
warnings.filterwarnings('ignore')
 print(os.listdir(PATH_KERAS))<load_from_csv>
features_selected=[fea for fea in X_train.columns if fea not in to_drop]
df_aptos = pd.read_csv(PATH_APTOS + 'train.csv')
 df_aptos.rename(index=str, columns={"id_code": "id_code_aptos"}, inplace=True )<load_from_csv>
pred_val = np.zeros(len(train))
pred_test = np.zeros(len(test))
for foldIdx,(trn_idx, val_idx)in enumerate(kfold.split(X_train.loc[:,features_selected], y_train)) :
print("Fold {}".format(foldIdx))
lgbm=lgb.LGBMClassifier(n_estimators=100000,random_state=random_state,**param)
 lgbm.fit(X_train.iloc[trn_idx][features_selected],y_train[trn_idx],
eval_set=[(X_train.iloc[trn_idx][features_selected],y_train[trn_idx]),(X_train.iloc[val_idx][features_selected],y_train[val_idx])],
early_stopping_rounds = 3000,
verbose=5000)
 pred_val[val_idx] = lgbm.predict_proba(X_train.loc[val_idx,features_selected], num_iteration=lgbm.best_iteration_)[:,1]
pred_test += lgbm.predict_proba(X_test[features_selected],num_iteration=lgbm.best_iteration_)[:,1] / kfold.n_splits
df_2015 = pd.read_csv(PATH_2015 + 'trainLabels.csv')
 df_2015.rename(index=str, columns={"image": "id_code_2015", "level": "diagnosis"}, inplace=True)
 df_2015 = df_2015.drop(df_2015[df_2015['diagnosis'] == 0].sample(frac=0.8 ).index )<define_variables>
print('AUC score: %.5f' % roc_auc_score(y_train,pred_val))
<set_options><EOS> 
submission_baseline = pd.DataFrame({'ID_code': test.ID_code.values,
'target':pred_test_baseline})
 submission_baseline.to_csv("LGBM_baseline.csv", index=False)
 submission = pd.DataFrame({'ID_code': test.ID_code.values,
'target':pred_test})
 submission.to_csv("LGBM_V3.csv", index=False )