Kaggle competition: Home Credit Default Risk
def build_model_2(weights_path=None):
model = Sequential()
model.add(Conv2D(16,(3,3), activation='relu', padding='same', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
 model.add(Conv2D(16,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(len(CATEGORIES),(1, 1)))
 model.add(GlobalAveragePooling2D())
 model.add(Activation('softmax'))
if weights_path is not None:
model.load_weights(weights_path)
 model.compile(Adam() , loss='categorical_crossentropy', metrics=['categorical_accuracy'])
 return model<choose_model_class>
df = pd.read_pickle('.. /input/save-dromosys-features/df.pkl.gz')
 print("Raw shape: ", df.shape)
 y = df['TARGET']
feats = [f for f in df.columns if f not in ['TARGET','SK_ID_CURR','SK_ID_BUREAU','SK_ID_PREV','index']]
X = df[feats]
print("X shape: ", X.shape, " y shape:", y.shape)
 print("
Preparing data...")
 X = X.fillna(X.mean() ).clip(-1e11,1e11 )
def build_model_3(weights_path=None):
model = Sequential()
model.add(Conv2D(8,(3,3), activation='relu', padding='same', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
 model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(16,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(len(CATEGORIES), activation='softmax'))
if weights_path is not None:
model.load_weights(weights_path)
 model.compile(Adam() , loss='categorical_crossentropy', metrics=['categorical_accuracy'])
 return model<split>
def rank_gauss(x):
N = x.shape[0]
temp = x.argsort()
rank_x = temp.argsort() / N
rank_x -= rank_x.mean()
rank_x *= 2
efi_x = erfinv(rank_x)
 efi_x -= efi_x.mean()
return efi_x
for(i, final_model)in ensemble_final_models:
display(final_model.evaluate_generator(generator=val_generator, steps=val_steps))<predict_on_test>
for i in X.columns:
X[i] = rank_gauss(X[i].values )
for(i, final_model)in ensemble_final_models:
val_generator.reset()
val_predictions = final_model.predict_generator(
 val_generator,
steps=val_steps,
verbose=1 
)
 display(val_predictions.shape)
 val_predictions_labels = np.argmax(val_predictions, axis=1)
 val_true_labels = val_generator.classes[:val_predictions.shape[0]]
display(confusion_matrix(val_true_labels, val_predictions_labels))<choose_model_class>
training = y.notnull()
testing = y.isnull()
X_train = X[training].values
X_test = X[testing].values
y_train = np.array(y[training])
 print(X_train.shape, X_test.shape, y_train.shape)
 gc.collect()
model_input = Input(shape=(IMG_WIDTH, IMG_HEIGHT, 3))
yModels=[m(model_input)for i, m in ensemble_final_models]
overall_model = Model(
 model_input,
Average()(yModels),
name='ensemble'
)<predict_on_test>
class roc_callback(Callback):
def __init__(self,training_data,validation_data):
self.x = training_data[0]
self.y = training_data[1]
self.x_val = validation_data[0]
self.y_val = validation_data[1]
def on_train_begin(self, logs={}):
return
def on_train_end(self, logs={}):
return
def on_epoch_begin(self, epoch, logs={}):
return
def on_epoch_end(self, epoch, logs={}):
y_pred = self.model.predict(self.x)
 roc = roc_auc_score(self.y, y_pred)
 y_pred_val = self.model.predict(self.x_val)
 roc_val = roc_auc_score(self.y_val, y_pred_val)
 print('\rroc-auc: %s - roc-auc_val: %s' %(str(round(roc,4)) ,str(round(roc_val,4))),end=100*' '+'
')
 return
def on_batch_begin(self, batch, logs={}):
return
def on_batch_end(self, batch, logs={}):
return
test_generator.reset()
predictions = overall_model.predict_generator(
 test_generator,
steps=test_generator.n,
verbose=1 
)
 predicted_class_indices = np.argmax(predictions, axis=1)
 display(predicted_class_indices )<define_variables>
folds = KFold(n_splits=10, shuffle=True, random_state=42)
 sub_preds = np.zeros(X_test.shape[0])
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(X_train)) :
trn_x, trn_y = X_train[trn_idx], y_train[trn_idx]
val_x, val_y = X_train[val_idx], y_train[val_idx]
print('Setting up neural network...')
 nn = Sequential()
nn.add(Dense(units = 400 , kernel_initializer = 'normal', input_dim = 718))
nn.add(PReLU())
 nn.add(Dropout (.3))
nn.add(Dense(units = 160 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(units = 64 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(units = 26, kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(units = 12, kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
nn.compile(loss='binary_crossentropy', optimizer='adam')
 print('Fitting neural network...')
 nn.fit(trn_x, trn_y, validation_data =(val_x, val_y), epochs=10, verbose=2,
callbacks=[roc_callback(training_data=(trn_x, trn_y),validation_data=(val_x, val_y)) ])
 print('Predicting...')
 sub_preds += nn.predict(X_test ).flatten().clip(0,1)/ folds.n_splits
gc.collect()
<define_variables><EOS> 
print('Saving results...')
 sub = pd.DataFrame()
sub['SK_ID_CURR'] = df[testing]['SK_ID_CURR']
sub['TARGET'] = sub_preds
sub[['SK_ID_CURR', 'TARGET']].to_csv('sub_nn.csv', index= False)
 print(sub.head() )