Kaggle competition: Home Credit Default Risk
soldf.to_csv("submission.csv",sep=',',index=False )<set_options>
M1 = pd.read_csv('.. /input/diversity/LGBM.798.csv')
 M2 = pd.read_csv('.. /input/ingredients/WEIGHT_AVERAGE_RANK2.csv')
 M3 = pd.read_csv('.. /input/neural/sub_nn.csv')
 M4 = pd.read_csv('.. /input/genetic/pure_submission.csv')
 M5 = pd.read_csv('.. /input/diversity/xgb.796.csv' )
%matplotlib inline
mpl.style.use('ggplot')
 sns.set_style('white' )<load_from_csv>
def merge_dataframes(dfs, merge_keys):
dfs_merged = reduce(lambda left,right: pd.merge(left, right, on=merge_keys), dfs)
 return dfs_merged
sample = pd.read_csv('/kaggle/input/mlbio1/sample_submission.csv')
 test = pd.read_csv('/kaggle/input/mlbio1/test.csv')
 train = pd.read_csv('/kaggle/input/mlbio1/train.csv')
<sort_values>
dfs = [M1,M2,M3,M4,M5]
merge_keys=['SK_ID_CURR']
df = merge_dataframes(dfs, merge_keys=merge_keys )
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<drop_column>
df.columns = ['SK_ID_CURR','T1','T2','T3','T4','T5']
df.head()
train = train.drop('smoking_status', 1)
 test = test.drop('smoking_status', 1 )<feature_engineering>
pred_prob = 0.5 * df['T2'] + 0.5 * df['T1']
pred_prob.head()
median_bmi = train['bmi'].median()
train['bmi'] = train['bmi'].fillna(median_bmi)
 test['bmi'] = test['bmi'].fillna(median_bmi )<train_on_grid>
sub = pd.DataFrame()
sub['SK_ID_CURR'] = df['SK_ID_CURR']
sub['target']= pred_prob
def cross_validation_for_roc_auc(clf, X, y ,cv=5):
X = np.array(X.copy())
 y = np.array(y.copy())
 kf = KFold(n_splits=cv)
 kf.get_n_splits(X)
 scores = []
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
 prediction_on_this_fold = clf.predict_proba(X_test)[:,1]
score = roc_auc_score(y_score=prediction_on_this_fold, y_true=y_test)
 scores.append(score)
 return scores 
<compute_train_metric>
sub.to_csv('ldit.csv', index=False )
def calc_smooth_mean(train, test, by, on, m):
mean = train[on].mean()
agg = train.groupby(by)[on].agg(['count', 'mean'])
 counts = agg['count']
means = agg['mean']
smooth =(counts * means + m * mean)/(counts + m)
 return train[by].map(smooth), test[by].map(smooth )<compute_train_metric>
B_prob = 0.6 * df['T1'] + 0.2 * df['T3'] + 0.2 * df['T4']
for const in range(0, 21, 5):
lgb = LGBMClassifier(n_estimators=50, max_depth=3)
 sgd = SGDClassifier(loss='log', penalty = 'elasticnet')
 train['gender_target_enc'],test['gender_target_enc'] = \
calc_smooth_mean(train, test, by='gender',on='stroke', m=const)
 print(const, np.mean(cross_validation_for_roc_auc(lgb, train[['gender_target_enc']] , train['stroke'])) ,
np.mean(cross_validation_for_roc_auc(sgd, train[['gender_target_enc']] , train['stroke'])) )<drop_column>
SUB = pd.DataFrame()
SUB['SK_ID_CURR'] = df['SK_ID_CURR']
SUB['TARGET'] = B_prob
SUB.to_csv('Blendss.csv', index=False )
train = train.drop('gender_target_enc', 1)
 test = test.drop('gender_target_enc', 1)
 train = train.drop('gender', 1)
 test = test.drop('gender', 1 )<compute_train_metric>
corr_pred = 0.6 * df['T2'] + 0.05 * df['T3'] + 0.05 * df['T4'] + 0.1 * df['T5'] + 0.2 * df['T1']
corr_pred.head()
<feature_engineering><EOS> 
SuB = pd.DataFrame()
SuB['SK_ID_CURR'] = df['SK_ID_CURR']
SuB['TARGET'] = corr_pred
SuB.to_csv('corr_blend.csv', index=False )