Kaggle competition: Home Credit Default Risk
<SOS> metric: AUC Kaggle data source: home-credit-default-risk<randomize_order>
print('Importing data...')
 data = pd.read_csv('.. /input/home-credit-default-risk/application_train.csv')
 test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv')
 prev = pd.read_csv('.. /input/home-credit-default-risk/previous_application.csv')
 buro = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv')
 buro_balance = pd.read_csv('.. /input/home-credit-default-risk/bureau_balance.csv')
 credit_card = pd.read_csv('.. /input/home-credit-default-risk/credit_card_balance.csv')
 POS_CASH = pd.read_csv('.. /input/home-credit-default-risk/POS_CASH_balance.csv')
 payments = pd.read_csv('.. /input/home-credit-default-risk/installments_payments.csv')
 lgbm_submission = pd.read_csv('.. /input/home-credit-default-risk/sample_submission.csv')
 y = data['TARGET']
del data['TARGET']
categorical_features = [col for col in data.columns if data[col].dtype == 'object']
one_hot_df = pd.concat([data,test])
 one_hot_df = pd.get_dummies(one_hot_df, columns=categorical_features)
 data = one_hot_df.iloc[:data.shape[0],:]
test = one_hot_df.iloc[data.shape[0]:,]
print('Pre-processing buro_balance...')
 buro_grouped_size = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].size()
buro_grouped_max = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].max()
buro_grouped_min = buro_balance.groupby('SK_ID_BUREAU')['MONTHS_BALANCE'].min()
buro_counts = buro_balance.groupby('SK_ID_BUREAU')['STATUS'].value_counts(normalize = False)
 buro_counts_unstacked = buro_counts.unstack('STATUS')
 buro_counts_unstacked.columns = ['STATUS_0', 'STATUS_1','STATUS_2','STATUS_3','STATUS_4','STATUS_5','STATUS_C','STATUS_X',]
buro_counts_unstacked['MONTHS_COUNT'] = buro_grouped_size
buro_counts_unstacked['MONTHS_MIN'] = buro_grouped_min
buro_counts_unstacked['MONTHS_MAX'] = buro_grouped_max
buro = buro.join(buro_counts_unstacked, how='left', on='SK_ID_BUREAU')
 print('Pre-processing previous_application...')
 prev_cat_features = [pcol for pcol in prev.columns if prev[pcol].dtype == 'object']
prev = pd.get_dummies(prev, columns=prev_cat_features)
 avg_prev = prev.groupby('SK_ID_CURR' ).mean()
cnt_prev = prev[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
avg_prev['nb_app'] = cnt_prev['SK_ID_PREV']
del avg_prev['SK_ID_PREV']
print('Pre-processing buro...')
 buro_cat_features = [bcol for bcol in buro.columns if buro[bcol].dtype == 'object']
buro = pd.get_dummies(buro, columns=buro_cat_features)
 avg_buro = buro.groupby('SK_ID_CURR' ).mean()
avg_buro['buro_count'] = buro[['SK_ID_BUREAU', 'SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
del avg_buro['SK_ID_BUREAU']
print('Pre-processing POS_CASH...')
 le = LabelEncoder()
POS_CASH['NAME_CONTRACT_STATUS'] = le.fit_transform(POS_CASH['NAME_CONTRACT_STATUS'].astype(str))
nunique_status = POS_CASH[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).nunique()
nunique_status2 = POS_CASH[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).max()
POS_CASH['NUNIQUE_STATUS'] = nunique_status['NAME_CONTRACT_STATUS']
POS_CASH['NUNIQUE_STATUS2'] = nunique_status2['NAME_CONTRACT_STATUS']
POS_CASH.drop(['SK_ID_PREV', 'NAME_CONTRACT_STATUS'], axis=1, inplace=True)
 print('Pre-processing credit_card...')
 credit_card['NAME_CONTRACT_STATUS'] = le.fit_transform(credit_card['NAME_CONTRACT_STATUS'].astype(str))
nunique_status = credit_card[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).nunique()
nunique_status2 = credit_card[['SK_ID_CURR', 'NAME_CONTRACT_STATUS']].groupby('SK_ID_CURR' ).max()
credit_card['NUNIQUE_STATUS'] = nunique_status['NAME_CONTRACT_STATUS']
credit_card['NUNIQUE_STATUS2'] = nunique_status2['NAME_CONTRACT_STATUS']
credit_card.drop(['SK_ID_PREV', 'NAME_CONTRACT_STATUS'], axis=1, inplace=True)
 print('Pre-processing payments...')
 avg_payments = payments.groupby('SK_ID_CURR' ).mean()
avg_payments2 = payments.groupby('SK_ID_CURR' ).max()
avg_payments3 = payments.groupby('SK_ID_CURR' ).min()
del avg_payments['SK_ID_PREV']
print('Joining databases...')
 data = data.merge(right=avg_prev.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_prev.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_buro.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_buro.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(POS_CASH.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(POS_CASH.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(credit_card.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(credit_card.groupby('SK_ID_CURR' ).mean().reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_payments.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_payments2.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments2.reset_index() , how='left', on='SK_ID_CURR')
 data = data.merge(right=avg_payments3.reset_index() , how='left', on='SK_ID_CURR')
 test = test.merge(right=avg_payments3.reset_index() , how='left', on='SK_ID_CURR')
 print('Removing features with more than 80% missing...')
shuffle_and_test(10,20,X_At4_val )<randomize_order>
test = test[test.columns[data.isnull().mean() < 0.85]]
data = data[data.columns[data.isnull().mean() < 0.85]]
data.drop(columns=['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_12', 'FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14',
'FLAG_DOCUMENT_15', 'FLAG_DOCUMENT_16', 'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20',
'FLAG_DOCUMENT_21'],axis=1,inplace=True )
shuffle_and_test(20,30,X_At4_val )<train_model>
test.drop(columns=['FLAG_DOCUMENT_2', 'FLAG_DOCUMENT_10', 'FLAG_DOCUMENT_12', 'FLAG_DOCUMENT_13', 'FLAG_DOCUMENT_14',
'FLAG_DOCUMENT_15', 'FLAG_DOCUMENT_16', 'FLAG_DOCUMENT_17', 'FLAG_DOCUMENT_19', 'FLAG_DOCUMENT_20',
'FLAG_DOCUMENT_21'],axis=1,inplace=True )
model1=create_simple_mlp()
model1.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['acc'])
 history_model1 = model1.fit(X_At2_train,y_At2_train ,batch_size=16, validation_data=(X_At4_val, y_At4_val), epochs=400, callbacks=[es],verbose=0)
 plot_accuracy_and_loss(history_model1)
<predict_on_test>
data['DAYS_EMPLOYED_ANOM'] = data["DAYS_EMPLOYED"] == 365243
data['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True)
 test['DAYS_EMPLOYED_ANOM'] = test["DAYS_EMPLOYED"] == 365243
test['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True )
pred_at2=model1.predict(X_At4_val )<choose_model_class>
data['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True)
 test['DAYS_LAST_PHONE_CHANGE'].replace(0, np.nan, inplace=True )
model2 = Sequential()
for layer in model1.layers[:-1]:
model2.add(layer)
 model2.add(Dense(30, activation='selu'))
model2.add(Dense(10, activation='selu'))
model2.add(Dense(4, activation='softmax')) 
<train_model>
data['DAYS_CREDIT_ENDDATE'][data['DAYS_CREDIT_ENDDATE'] < -40000] = np.nan
test['DAYS_CREDIT_ENDDATE'][test['DAYS_CREDIT_ENDDATE'] < -40000] = np.nan
model2.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['acc'])
 history_model2 = model2.fit(X_At4_train,y_At4_train ,batch_size=64, validation_data=(X_At4_val, y_At4_val), epochs=100, callbacks=[es],verbose=0)
 plot_accuracy_and_loss(history_model2)
<create_dataframe>
data['DAYS_CREDIT_UPDATE'][data['DAYS_CREDIT_UPDATE'] < -40000] = np.nan
data['DAYS_ENDDATE_FACT'][data['DAYS_ENDDATE_FACT'] < -40000] = np.nan
test['DAYS_CREDIT_UPDATE'][test['DAYS_CREDIT_UPDATE'] < -40000] = np.nan
test['DAYS_ENDDATE_FACT'][test['DAYS_ENDDATE_FACT'] < -40000] = np.nan
def calculate_score(df):
output=pd.DataFrame()
for i in range(0,10):
sum_spikes=df.iloc[:,i]
latency=df.iloc[:,i+10] + 1.5
output[i]= sum_spikes/latency
for i in range(0,10):
output[i+10]=df.iloc[:,i+20]
return output<normalization>
data['AMT_DRAWINGS_ATM_CURRENT'][data['AMT_DRAWINGS_ATM_CURRENT'] < 0] = np.nan
data['AMT_DRAWINGS_CURRENT'][data['AMT_DRAWINGS_CURRENT'] < 0] = np.nan
test['AMT_DRAWINGS_ATM_CURRENT'][test['AMT_DRAWINGS_ATM_CURRENT'] < 0] = np.nan
test['AMT_DRAWINGS_CURRENT'][test['AMT_DRAWINGS_CURRENT'] < 0] = np.nan
df_copy=at4_train.copy()
train_values_to10 = df_copy.iloc[:,1:11].values
train_values_to20 = df_copy.iloc[:,11:21].values
train_values_to30= df_copy.iloc[:,21:31].values
min_max_scaler_spike = preprocessing.MinMaxScaler(feature_range=(0,1))
min_max_scaler_latency = preprocessing.MinMaxScaler(feature_range=(0,1))
min_max_scaler_rank = preprocessing.MinMaxScaler(feature_range=(0,1))
transformation_to10 = min_max_scaler_spike.fit(train_values_to10.T)
 train_values_scaled_10 = min_max_scaler_spike.transform(train_values_to10.T ).T
transformation_to20 = min_max_scaler_latency.fit(train_values_to20.T)
 train_values_scaled_20 = min_max_scaler_latency.transform(train_values_to20.T ).T
transformation_to30 = min_max_scaler_rank.fit(train_values_to30.T)
 train_values_scaled_30 = min_max_scaler_rank.transform(train_values_to30.T ).T
train_scaled=pd.DataFrame()
train_scaled['label']=at4_train['label']
for i in range(1,11):
train_scaled['onsc'+str(i)]=pd.DataFrame(train_values_scaled_10 ).iloc[:,i-1]
for i in range(1,11):
train_scaled['onstf'+str(i)]=pd.DataFrame(train_values_scaled_20 ).iloc[:,i-1]
for i in range(1,11):
train_scaled['onsto'+str(i)]=pd.DataFrame(train_values_scaled_30 ).iloc[:,i-1]<split>
train_labels = y
data, test = data.align(test, join = 'inner', axis = 1)
 data['TARGET'] = y
print('Training Features shape: ', data.shape)
 print('Testing Features shape: ', test.shape )
X_train_30, X_val_30, y_train, y_val=train_test_split(train_scaled.loc[: , train_scaled.columns!='label'],train_scaled['label'],random_state=0,test_size=0.2 )<compute_test_metric>
test['DAYS_EMPLOYED_ANOM'] = test["DAYS_EMPLOYED"] == 365243
test["DAYS_EMPLOYED"].replace({365243: np.nan}, inplace = True)
 print('There are %d anomalies in the test data out of %d entries' %(test["DAYS_EMPLOYED_ANOM"].sum() , len(test)) )
X_train=calculate_score(X_train_30)
 X_val=calculate_score(X_val_30 )<prepare_x_and_y>
poly_features = data[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3', 'DAYS_BIRTH', 'TARGET']]
poly_features_test = test[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3', 'DAYS_BIRTH']]
imputer = SimpleImputer(strategy = 'median')
 poly_target = poly_features['TARGET']
poly_features = poly_features.drop(columns = ['TARGET'])
 poly_features = imputer.fit_transform(poly_features)
 poly_features_test = imputer.transform(poly_features_test)
 poly_transformer = PolynomialFeatures(degree = 3 )
y_cross_validation=train_scaled.iloc[:,0]
X_cross_validation=calculate_score(train_scaled.iloc[:,1:] )<choose_model_class>
poly_transformer.fit(poly_features)
 poly_features = poly_transformer.transform(poly_features)
 poly_features_test = poly_transformer.transform(poly_features_test)
 print('Polynomial Features shape: ', poly_features.shape )
def create_model() :
model = Sequential()
model.add(Dense(30,input_dim=20, activation='swish'))
model.add(Dropout(0.1))
model.add(Dense(50,activation='swish'))
model.add(Dropout(0.1))
model.add(Dense(30,activation='swish'))
model.add(Dropout(0.1))
model.add(Dense(10,activation='swish'))
model.add(Dropout(0.1))
model.add(Dense(4, activation='softmax'))
opt = keras.optimizers.Adam(learning_rate=0.004)
 model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['acc'])
 return model<choose_model_class>
poly_transformer.get_feature_names(input_features = ['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3', 'DAYS_BIRTH'])[:15]
nn=create_model()
es = EarlyStopping(monitor='val_loss', mode='auto', verbose=1,patience=25)
<choose_model_class>
poly_features = pd.DataFrame(poly_features,
columns = poly_transformer.get_feature_names(['EXT_SOURCE_1', 'EXT_SOURCE_2',
'EXT_SOURCE_3', 'DAYS_BIRTH']))
poly_features['TARGET'] = poly_target
poly_corrs = poly_features.corr() ['TARGET'].sort_values()
print(poly_corrs.head(20))
print(poly_corrs.tail(20))
NN_clf=KerasClassifier(build_fn=create_model, epochs=200, batch_size= 16,verbose=0)
 NN_clf._estimator_type = "classifier"<compute_test_metric>
poly_features_test = pd.DataFrame(poly_features_test,
columns = poly_transformer.get_feature_names(['EXT_SOURCE_1', 'EXT_SOURCE_2',
'EXT_SOURCE_3', 'DAYS_BIRTH']))
poly_features['SK_ID_CURR'] = data['SK_ID_CURR']
app_train_poly = data.merge(poly_features, on = 'SK_ID_CURR', how = 'left')
 poly_features_test['SK_ID_CURR'] = test['SK_ID_CURR']
app_test_poly = test.merge(poly_features_test, on = 'SK_ID_CURR', how = 'left')
 app_train_poly, app_test_poly = app_train_poly.align(app_test_poly, join = 'inner', axis = 1)
 print('Training data with polynomial features shape: ', app_train_poly.shape)
 print('Testing data with polynomial features shape: ', app_test_poly.shape )
scores_cross_validation = cross_val_score(NN_clf,X_cross_validation, y_cross_validation, cv=5 )<train_model>
app_train_poly.head()
app_train_poly=app_train_poly.rename(columns={'EXT_SOURCE_1_x':'EXT_SOURCE_1','EXT_SOURCE_2_x':'EXT_SOURCE_2','EXT_SOURCE_3_x':'EXT_SOURCE_3','DAYS_BIRTH_x':'DAYS_BIRTH'})
 app_test_poly=app_test_poly.rename(columns={'EXT_SOURCE_1_x':'EXT_SOURCE_1','EXT_SOURCE_2_x':'EXT_SOURCE_2','EXT_SOURCE_3_x':'EXT_SOURCE_3','DAYS_BIRTH_x':'DAYS_BIRTH'} )
history_final = nn.fit(X_train,y_train ,batch_size=64, validation_data=(X_val, y_val), epochs=200, callbacks=[es],verbose=0)
 plot_accuracy_and_loss(history_final)
<choose_model_class>
app_test_poly.drop('1',axis=1,inplace=True)
 app_train_poly.drop('1',axis=1,inplace=True )
estimators = [('NN', NN_clf), 
('svm', BaggingClassifier(base_estimator=SVC() , n_estimators=20, random_state=0)) , 
('rforest', ExtraTreesClassifier(n_estimators=50, random_state=0,min_samples_split=4)) , 
('grad_boost',GradientBoostingClassifier(n_estimators=200)) ]<choose_model_class>
colsToRemove = []
for col in app_train_poly.columns:
if col != 'SK_ID_CURR' and col != 'TARGET':
if app_train_poly[col].std() == 0:
colsToRemove.append(col)
 app_train_poly.drop(colsToRemove, axis=1, inplace=True)
 app_test_poly.drop(colsToRemove, axis=1, inplace=True)
 print("Removed `{}` Constant Columns
".format(len(colsToRemove)))
 print(colsToRemove )
estmiators_to_cross_validate=[NN_clf,BaggingClassifier(base_estimator=SVC() , n_estimators=20, random_state=0),ExtraTreesClassifier(n_estimators=50, random_state=0,min_samples_split=4),GradientBoostingClassifier(n_estimators=200)]<choose_model_class>
reg = StackingClassifier(estimators=estimators,final_estimator=LogisticRegression())
 reg._estimator_type = "classifier"<compute_train_metric>
app_train_poly=app_train_poly.rename(columns={'AMT_CREDIT_x':'AMT_CREDIT','AMT_GOODS_PRICE_x':'AMT_GOODS_PRICE'})
 app_test_poly=app_test_poly.rename(columns={'AMT_CREDIT_x':'AMT_CREDIT','AMT_GOODS_PRICE_x':'AMT_GOODS_PRICE'} )
for c_model in estmiators_to_cross_validate:
scores_cross_validation_reg = cross_val_score(c_model,X_cross_validation, y_cross_validation, cv=5)
 print(scores_cross_validation_reg )<train_model>
app_train_domain = app_train_poly.copy()
app_test_domain = app_test_poly.copy()
app_train_domain['CREDIT_INCOME_PERCENT'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_INCOME_TOTAL']
app_train_domain['ANNUITY_INCOME_PERCENT'] = app_train_domain['AMT_ANNUITY_x'] / app_train_domain['AMT_INCOME_TOTAL']
app_train_domain['CREDIT_TERM'] = app_train_domain['AMT_ANNUITY_x'] / app_train_domain['AMT_CREDIT']
app_train_domain['DAYS_EMPLOYED_PERCENT'] = app_train_domain['DAYS_EMPLOYED'] / app_train_domain['DAYS_BIRTH']
app_train_domain['NEW_CREDIT_TO_ANNUITY_RATIO'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_ANNUITY_x']
app_train_domain['NEW_CREDIT_TO_GOODS_RATIO'] = app_train_domain['AMT_CREDIT'] / app_train_domain['AMT_GOODS_PRICE']
app_train_domain['NEW_EXT_SOURCES_MEAN'] = app_train_domain[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis=1)
 app_train_domain['NEW_EMPLOY_TO_BIRTH_RATIO'] = app_train_domain['DAYS_EMPLOYED'] / app_train_domain['DAYS_BIRTH']
reg.fit(X_train,y_train ).score(X_val,y_val)
<compute_test_metric>
app_test_domain['CREDIT_INCOME_PERCENT'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_INCOME_TOTAL']
app_test_domain['ANNUITY_INCOME_PERCENT'] = app_test_domain['AMT_ANNUITY_x'] / app_test_domain['AMT_INCOME_TOTAL']
app_test_domain['CREDIT_TERM'] = app_test_domain['AMT_ANNUITY_x'] / app_test_domain['AMT_CREDIT']
app_test_domain['DAYS_EMPLOYED_PERCENT'] = app_test_domain['DAYS_EMPLOYED'] / app_test_domain['DAYS_BIRTH']
app_test_domain['NEW_CREDIT_TO_ANNUITY_RATIO'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_ANNUITY_x']
app_test_domain['NEW_CREDIT_TO_GOODS_RATIO'] = app_test_domain['AMT_CREDIT'] / app_test_domain['AMT_GOODS_PRICE']
app_test_domain['NEW_EXT_SOURCES_MEAN'] = app_test_domain[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis=1)
 app_test_domain['NEW_EMPLOY_TO_BIRTH_RATIO'] = app_test_domain['DAYS_EMPLOYED'] / app_test_domain['DAYS_BIRTH']
reg.score(X_train,y_train )<load_from_csv>
app_train_domain['TARGET'] = poly_target
print('Training data with polynomial features shape: ', app_train_domain.shape)
 print('Testing data with polynomial features shape: ', app_test_domain.shape )
test_data =pd.read_csv(at4_test_path,sep="," )<normalization>
app_train_domain['TARGET'] = poly_target
print('Training data with polynomial features shape: ', app_train_domain.shape)
 print('Testing data with polynomial features shape: ', app_test_domain.shape )
test_copy=test_data.copy()
test_min_max_scaler_spike = preprocessing.MinMaxScaler(feature_range=(0,1))
test_min_max_scaler_latency = preprocessing.MinMaxScaler(feature_range=(0,1))
test_min_max_scaler_rank = preprocessing.MinMaxScaler(feature_range=(0,1))
test_values_to10 = test_copy.iloc[:,0:10].values
test_values_to20 = test_copy.iloc[:,10:20].values
test_values_to30= test_copy.iloc[:,20:30].values
test_transformation_to10 = test_min_max_scaler_spike.fit(test_values_to10.T)
 test_values_scaled_10 = test_min_max_scaler_spike.transform(test_values_to10.T ).T
test_transformation_to20 = test_min_max_scaler_latency.fit(test_values_to20.T)
 test_values_scaled_20 = test_min_max_scaler_latency.transform(test_values_to20.T ).T
test_transformation_to30 = test_min_max_scaler_rank.fit(test_values_to30.T)
 test_values_scaled_30 = test_min_max_scaler_rank.transform(test_values_to30.T ).T
test_scaled=pd.DataFrame()
for i in range(0,10):
test_scaled['onsc'+str(i)]=pd.DataFrame(test_values_scaled_10 ).iloc[:,i]
for i in range(0,10):
test_scaled['onstf'+str(i)]=pd.DataFrame(test_values_scaled_20 ).iloc[:,i]
for i in range(0,10):
test_scaled['onsto'+str(i)]=pd.DataFrame(test_values_scaled_30 ).iloc[:,i]<compute_test_metric>
app_train_domain = app_train_domain.drop('SK_ID_PREV_x',axis=1)
 app_test_domain = app_test_domain.drop('SK_ID_PREV_x',axis=1 )
X_test=calculate_score(test_scaled)
<predict_on_test>
app_train_domain = app_train_domain.drop('SK_ID_PREV_y',axis=1)
 app_test_domain = app_test_domain.drop('SK_ID_PREV_y',axis=1 )
test_predictions=reg.predict(X_test)
 test_predictions<define_variables>
def model(features, test_features, encoding = 'ohe', n_folds =5):

 train_ids = features['SK_ID_CURR']
test_ids = test_features['SK_ID_CURR']
labels = features['TARGET']
features = features.drop(columns = ['SK_ID_CURR', 'TARGET'])
 test_features = test_features.drop(columns = ['SK_ID_CURR'])
 if encoding == 'ohe':
features = pd.get_dummies(features)
 test_features = pd.get_dummies(test_features)
 features, test_features = features.align(test_features, join = 'inner', axis = 1)
 cat_indices = 'auto'
elif encoding == 'le':
label_encoder = LabelEncoder()
cat_indices = []
for i, col in enumerate(features):
if features[col].dtype == 'object':
features[col] = label_encoder.fit_transform(np.array(features[col].astype(str)).reshape(( -1,)))
 test_features[col] = label_encoder.transform(np.array(test_features[col].astype(str)).reshape(( -1,)))
 cat_indices.append(i)
 else:
raise ValueError("Encoding must be either 'ohe' or 'le'")
 print('Training Data Shape: ', features.shape)
 print('Testing Data Shape: ', test_features.shape)
 feature_names = list(features.columns)
 features = np.array(features)
 test_features = np.array(test_features)
 k_fold = KFold(n_splits = n_folds, shuffle = True, random_state = 50)
 feature_importance_values = np.zeros(len(feature_names))
test_predictions = np.zeros(test_features.shape[0])
 out_of_fold = np.zeros(features.shape[0])
 valid_scores = []
train_scores = []
for train_indices, valid_indices in k_fold.split(features):
train_features, train_labels = features[train_indices], labels[train_indices]
valid_features, valid_labels = features[valid_indices], labels[valid_indices]
model = lgb.LGBMClassifier(n_estimators=10000, nthread=4,objective = 'binary',
class_weight = 'balanced', learning_rate = 0.015,
reg_alpha = 0.041545473, reg_lambda = 0.1,
n_jobs = -1, random_state = 50,num_leaves=32,colsample_bytree=.9497036,subsample=.8715623,
max_depth=5,min_split_gain=.0222415,min_child_weight=39.3259775,max_bin=200,num_boost_round=3000,min_data_in_leaf=100,bagging_fraction=0.5,bagging_freq=10)
 model.fit(train_features, train_labels, eval_metric = 'auc',
eval_set = [(valid_features, valid_labels),(train_features, train_labels)],
eval_names = ['valid', 'train'], categorical_feature = cat_indices,
early_stopping_rounds = 100, verbose = 200)
 best_iteration = model.best_iteration_
feature_importance_values += model.feature_importances_ / k_fold.n_splits
test_predictions += model.predict_proba(test_features, num_iteration = best_iteration)[:, 1] / k_fold.n_splits
out_of_fold[valid_indices] = model.predict_proba(valid_features, num_iteration = best_iteration)[:, 1]
valid_score = model.best_score_['valid']['auc']
train_score = model.best_score_['train']['auc']
valid_scores.append(valid_score)
 train_scores.append(train_score)
 gc.enable()
del model, train_features, valid_features
gc.collect()
submission = pd.DataFrame({'SK_ID_CURR': test_ids, 'TARGET': test_predictions})
 feature_importances = pd.DataFrame({'feature': feature_names, 'importance': feature_importance_values})
 valid_auc = roc_auc_score(labels, out_of_fold)
 valid_scores.append(valid_auc)
 train_scores.append(np.mean(train_scores))
fold_names = list(range(n_folds))
fold_names.append('overall')
 metrics = pd.DataFrame({'fold': fold_names,
'train': train_scores,
'valid': valid_scores})
 return submission, feature_importances, metrics
l=len(test_predictions)
 result_array=[]
for i in range(0,l):
result_array.append([i,test_predictions[i]] )<create_dataframe>
submission, fi, metrics = model(app_train_domain, app_test_domain)
 print('Baseline metrics')
 print(metrics )
results = pd.DataFrame(result_array,columns=["id","label"] )<save_to_csv>
submission.to_csv('second_sub.csv', index = False )