Kaggle competition: Santander Customer Transaction Prediction
print("Current train size:",X_tr.shape)
 model = Sequential()
model.add(base)
 model.add(Flatten())
 model.add(Dense(256, use_bias=True))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(256,activation='relu'))
model.add(BatchNormalization())
 model.add(Dense(16, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
 model.summary()
callback=[keras.callbacks.EarlyStopping(monitor='val_acc', patience=20, verbose=1, mode='auto', restore_best_weights=True),
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto')]
model.fit(X_tr,Y_tr,batch_size=64, epochs=80, verbose=1, validation_split=0.1,callbacks=callback)
<save_to_csv>
test = pd.read_csv(".. /input/test.csv")
 train = pd.read_csv(".. /input/train.csv" )
test_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 X_test = []
imges = test_df['id'].values
for img_id in tqdm_notebook(imges):
X_test.append(cv2.imread(test_dir + img_id))
X_test = np.asarray(X_test)
 X_test = X_test.astype('float32')
 X_test /= 255
y_test_pred = model.predict_proba(X_test)
 test_df['has_cactus'] = y_test_pred
test_df.to_csv('tf_learning_vgg16_aug2_80epoch.csv', index=False )<import_modules>
ytrain = train['target']
xtrain = train.iloc[:,2:]
xtest = test.iloc[:,1:]
from PIL import Image
import numpy as np
import pandas as pd
from tqdm import tqdm
from keras.preprocessing.image import ImageDataGenerator
import keras
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,Dropout,MaxPooling2D,Activation, BatchNormalization, GlobalAveragePooling2D
from sklearn.model_selection import train_test_split
from keras.optimizers import Adam, SGD
from keras.callbacks import CSVLogger, ModelCheckpoint, ReduceLROnPlateau
from keras.regularizers import l2
import matplotlib.pyplot as plt<load_from_csv>
rus = RandomUnderSampler(random_state=1, replacement=True)
 xtrain1,ytrain1 = rus.fit_sample(xtrain, ytrain)
def load_data(dataframe=None, batch_size=16, mode='categorical'):
if dataframe is None:
dataframe = pd.read_csv('.. /input/train.csv')
 dataframe['has_cactus'] = dataframe['has_cactus'].apply(str)
 gen = ImageDataGenerator(rescale=1./255., validation_split=0.1, horizontal_flip=True, vertical_flip=True)
 trainGen = gen.flow_from_dataframe(dataframe, directory='.. /input/train/train/', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='validation')
 testGen = gen.flow_from_dataframe(dataframe, directory='.. /input/train/train/', x_col='id',y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='validation')
 return trainGen, testGen<define_variables>
param = {
'num_leaves': 2,
'learning_rate': 0.1,
'feature_fraction': 0.2,
'max_depth': -1,
'objective': 'binary',
'boosting_type': 'gbdt',
'metric': 'auc',
}
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=4590)
 oof = np.zeros(len(xtrain))
ypred = np.zeros(len(xtest))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(xtrain1,ytrain1)) :
print("fold {}".format(fold_))
rus = RandomUnderSampler(random_state=fold_, replacement=True)
 xtrain1,ytrain1 = rus.fit_sample(xtrain, ytrain)
 trn_data = lgb.Dataset(xtrain1[trn_idx], label=ytrain1[trn_idx])
 val_data = lgb.Dataset(xtrain1[val_idx], label=ytrain1[val_idx])
 num_round = 30000
model = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 300)
 oof += model.predict(xtrain)/ 5
ypred += model.predict(xtest)/ 5
roc_auc_score(ytrain,oof )
trainGen, valGen = load_data(batch_size=32 )<choose_model_class>
df = pd.DataFrame({'ID_code':test['ID_code'],'target':ypred})
 df.to_csv('undersamping.csv',index=None )
def train_model() :
model = Sequential()
model.add(Conv2D(32,(3, 3), padding='same',
input_shape=(32, 32, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3, 3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(128,(3, 3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dense(16))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.3))
model.add(Dense(2))
model.add(BatchNormalization())
 model.add(Activation('softmax'))
return model<train_model>
roc_auc_score(ytrain,oof )