Kaggle competition: House pricing
<SOS> metric: mape Kaggle data source: house-pricing<concatenate>
modelling_data_feat = np.load('.. /input/x_train.npy')
 modelling_data_target = np.load('.. /input/y_train.npy')
 submit_data_feat = np.load('.. /input/x_test.npy')
 df_mod_feat = pd.DataFrame(data=modelling_data_feat)
 df_sub_feat = pd.DataFrame(data=submit_data_feat)
 rs = 4319
all_text = pd.concat([train_pruned.drop(['target'], axis=1), val_pruned.drop(['target'], axis=1), test_pruned], ignore_index=True )<feature_engineering>
df_mod_feat = df_mod_feat.drop(columns=['waterfront','lat','long'])
 df_sub_feat = df_sub_feat.drop(columns=['waterfront','lat','long'])
 df_mod_feat_cat = df_mod_feat[['zipcode']].copy()
df_mod_feat_num = df_mod_feat.drop(columns=['zipcode'])
 df_sub_feat_cat = df_sub_feat[['zipcode']].copy()
df_sub_feat_num = df_sub_feat.drop(columns=['zipcode'])
 cat_allfeat = pd.concat([df_mod_feat_cat, df_sub_feat_cat])
 enc = OneHotEncoder()
temp = enc.fit_transform(cat_allfeat)
 temp = pd.DataFrame(temp.todense())
 df_mod_feat_cat_conv = temp.iloc[:df_mod_feat_cat.shape[0]]
df_sub_feat_cat_conv = temp.iloc[df_mod_feat_cat.shape[0]:cat_allfeat.shape[0]]
df_sub_feat_cat_conv = df_sub_feat_cat_conv.reset_index(drop=True)
 for i in range(0, df_mod_feat_num.shape[0]):
df_mod_feat_num.loc[i,'date'] = int(df_mod_feat_num.loc[i,'date'][0:4])
 if df_mod_feat_num.loc[i,'yr_renovated'] == 0:
df_mod_feat_num.loc[i,'yr_renovated'] = df_mod_feat_num.loc[i,'yr_built']
for i in range(0, df_sub_feat_num.shape[0]):
df_sub_feat_num.loc[i,'date'] = int(df_sub_feat_num.loc[i,'date'][0:4])
 if df_sub_feat_num.loc[i,'yr_renovated'] == 0:
df_sub_feat_num.loc[i,'yr_renovated'] = df_sub_feat_num.loc[i,'yr_built']
df_mod_feat = pd.concat([df_mod_feat_cat_conv, df_mod_feat_num], axis=1)
 df_sub_feat = pd.concat([df_sub_feat_cat_conv, df_sub_feat_num], axis=1)
cv = CountVectorizer(ngram_range=(1,2))
tfidf_transformer = TfidfTransformer()
counts = cv.fit_transform(all_text['text'])
 tfidf = tfidf_transformer.fit_transform(counts )<concatenate>
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs(( y_true - y_pred)/ y_true)) * 100
X_train, X_test, y_train, y_test = train_test_split(df_mod_feat, modelling_data_target, test_size=0.1, random_state=rs)
 model = LinearRegression(fit_intercept=False)
 selector = RFECV(estimator=model, step=1, min_features_to_select=1, cv=StratifiedKFold(n_splits=5, random_state=rs),scoring='r2')
 feat_res = selector.fit(X_train, y_train)
 print("Optimal number of features : %d" % selector.n_features_)
 sel_X_train = feat_res.transform(X_train)
 sel_X_test = feat_res.transform(X_test)
 y_true, y_pred = y_test, feat_res.estimator_.predict(sel_X_test)
 mean_absolute_percentage_error(y_true, y_pred )
<prepare_x_and_y><EOS> 
submission_features = feat_res.transform(df_sub_feat)
 test_predictions = feat_res.estimator_.predict(submission_features)
 submission = pd.DataFrame({'Id': range(1, test_predictions.shape[0]+1), 'Price': test_predictions})
 submission = submission.reset_index(drop=True)
 submission.to_csv('submission.csv', index=False)
 submission