Kaggle competition: Home Credit Default Risk
def get_f1_by_sample_lr(mult, is_sample=True):
if is_sample:
X_train_sample, y_train_sample = select_sample(mult)
 else:
X_train_sample, y_train_sample = X_train, y_train
lrclassifier = LogisticRegression()
lrclassifier.fit(X_train_sample, y_train_sample)
 train_f1_score, valid_f1_score = get_f1_score(lrclassifier, X_train_sample, X_valid, y_train_sample, y_valid)
 return {'sample_frac': X_train_sample.shape[0]/X_train.shape[0], 'train_f1_score': train_f1_score, 'valid_f1_score': valid_f1_score} 
<define_variables>
df = pd.read_pickle('.. /input/save-dromosys-features/df.pkl.gz')
 print("Raw shape: ", df.shape)
 df.set_index('SK_ID_CURR', inplace=True)
 y = df['TARGET'].copy()
feats = [f for f in df.columns if f not in ['TARGET','SK_ID_CURR','SK_ID_BUREAU','SK_ID_PREV','index']]
df.drop(['index', 'TARGET'], axis=1, inplace=True)
 print("X shape: ", df.shape, " y shape:", y.shape)
 print("
Preparing data...")
 for feat in feats:
df[feat] = df[feat].fillna(df[feat].mean() )
num_mults = int(sum(y_train==0)/sum(y_train==1))<categorify>
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
f1_mult_lr = []
mult = 1
while mult < num_mults:
f1_mult_lr.append(get_f1_by_sample_lr(mult))
if mult==1:
mult+=9
else:
mult+=10
f1_mult_lr.append(get_f1_by_sample_lr(_, is_sample=False))<normalization>
for i in df.columns:
df[i] = rank_gauss(df[i].values )
f1_mult_lr_df = pd.io.json.json_normalize(f1_mult_lr )<train_model>
training = y.notnull()
testing = y.isnull()
X_train = df[training].values
X_test = df[testing].values
y_train = np.array(y[training])
 print(X_train.shape, X_test.shape, y_train.shape)
 gc.collect()
def get_f1_by_sample_rf(mult, is_sample=True):
if is_sample:
X_train_sample, y_train_sample = select_sample(mult)
 else:
X_train_sample, y_train_sample = X_train, y_train
rfclassifier = RandomForestClassifier(n_estimators = 50, max_features = 10)
 rfclassifier.fit(X_train_sample, y_train_sample)
 train_f1_score, valid_f1_score = get_f1_score(rfclassifier, X_train_sample, X_valid, y_train_sample, y_valid)
 return {'sample_frac': X_train_sample.shape[0]/X_train.shape[0], 'train_f1_score': train_f1_score, 'valid_f1_score': valid_f1_score} 
<concatenate>
class IntervalEvaluation(Callback):
def __init__(self, validation_data=() , interval=10):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
y_pred = self.model.predict_proba(self.X_val, verbose=0)
 score = roc_auc_score(self.y_val, y_pred)
 logging.info("interval evaluation - epoch: {:d} - score: {:.6f}".format(epoch, score))
print("interval evaluation - epoch: {:d} - score: {:.6f}".format(epoch, score))
logs['val_auc'] = score
f1_mult_rf = []
mult = 1
while mult < num_mults:
f1_mult_rf.append(get_f1_by_sample_rf(mult))
if mult==1:
mult+=9
else:
mult+=10
f1_mult_rf.append(get_f1_by_sample_rf(_, is_sample=False))<normalization>
n_folds = 10
folds = KFold(n_splits=n_folds, shuffle=True, random_state=42)
 sub_preds = np.zeros(X_test.shape[0])
 oof_preds = np.zeros(X_train.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]
earlystop = EarlyStopping(monitor='val_auc', min_delta=0, patience=3, verbose=0, mode='max')
 file_path = "fold " + str(n_fold+1)+ " best_model.hdf5"
check_point = ModelCheckpoint(file_path, monitor = "val_auc", verbose = 1, save_best_only = True, mode = "max")
 print('Setting up neural network...')
 nn = Sequential()
nn.add(Dense(units = 400 , kernel_initializer = 'normal', input_dim = df.shape[1]))
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...')
 ival = IntervalEvaluation(validation_data=(val_x, val_y), interval=10)
 nn.fit(trn_x, trn_y, validation_data =(val_x, val_y), epochs=20, verbose=0, callbacks=[ival, earlystop, check_point], batch_size=128)
 best_model = load_model(file_path)
 oof_preds[val_idx] = best_model.predict(val_x ).flatten()
print(roc_auc_score(val_y, oof_preds[val_idx]))
print('Predicting...')
 sub_preds += best_model.predict(X_test ).flatten().clip(0,1)/ folds.n_splits
gc.collect()
print('FULL AUC: {}'.format(roc_auc_score(y_train, oof_preds)) )
f1_mult_rf_df = pd.io.json.json_normalize(f1_mult_rf )<train_model>
print('Saving results...')
 sub = pd.DataFrame()
sub_train = pd.DataFrame()
sub['SK_ID_CURR'] = df[testing].index
sub['TARGET'] = sub_preds
sub[['SK_ID_CURR', 'TARGET']].to_csv('sub_nn.csv', index= False)
 print(sub.head() )