Kaggle competition: House Prices - Advanced Regression Techniques
def Stacking_trainer(tr, te, cols, clf, NAME, _scaler=False):
_tr, _te = tr.copy() , te.copy()
cv = StratifiedKFold(n_splits=5, random_state=46)
 X = _tr[cols].values
Y = _tr['target'].values
oof = np.zeros(len(tr))
preds = np.zeros(len(te))
_te = _te[cols]
if _scaler:
scaler = StandardScaler()
X = scaler.fit_transform(X)
 _te = scaler.transform(_te)
 for trn_index, val_index in cv.split(X, Y):
X_trn, Y_trn = X[trn_index], Y[trn_index]
X_val, Y_val = X[val_index], Y[val_index]
clf.fit(X_trn, Y_trn)
 oof[val_index] = clf.predict_proba(X_val)[:,1]
preds += clf.predict_proba(_te)[:,1] / cv.n_splits
tr[NAME] = oof
te[NAME] = preds
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 4))
sns.distplot(tr[NAME], ax=axes[0])
 sns.distplot(te[NAME], ax=axes[1])
 axes[0].set_title('oof')
 axes[1].set_title('pred')
 plt.show()
print(NAME)
 print('AUC: {:.5f}'.format(roc_auc_score(tr['target'].values, oof)))
 return tr, te<define_variables>
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))
tr_stage0 = tr.copy()
col_not_rank = [col for col in tr_stage0.columns if col not in ['target'] and 'rank' not in col]
col_in_rank = [col for col in tr_stage0.columns if col not in ['target'] and 'rank' in col]<choose_model_class>
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['Electrical'], axis=1, inplace=True )
%%time
stack_stage1_dict = {
'Stack_LR_1': LogisticRegression(penalty='l2', solver='liblinear', random_state=1),
'Stack_LR_2': LogisticRegression(penalty='l2', solver='liblinear', random_state=10),
'Stack_LR_3': LogisticRegression(penalty='l2', solver='liblinear', random_state=100),
'Stack_LR_4': LogisticRegression(penalty='l2', solver='liblinear', random_state=1000)
 }
for name, clf in stack_stage1_dict.items() :
tr, te = Stacking_trainer(tr, te, col_in_rank, clf, name, _scaler=False )<feature_engineering>
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()
tr['Stack_LR_Mean'] =(tr['Stack_LR_1'] + tr['Stack_LR_2'] + tr['Stack_LR_3'] + tr['Stack_LR_4'])/ 4
te['Stack_LR_Mean'] =(te['Stack_LR_1'] + te['Stack_LR_2'] + te['Stack_LR_3'] + te['Stack_LR_4'])/ 4<statistical_test>
submission.to_csv('result-with-best.csv', index=False )