text_plain_output_2.png text_plain_output_1.png
from preprocess import DataPreprocessModule

from sklearn.linear_model import (LinearRegression, Lasso,

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import GridSearchCV

import numpy as np

import time

data_preprocess_module = DataPreprocessModule(train_path='../input/hdb-resale-price-prediction/train.csv', test_path='../input/hdb-resale-price-prediction/test.csv')
X_train, X_val, X_test, y_train, y_val = data_preprocess_module.get_preprocessed_data()

metric = lambda y1_real, y2_real: np.sqrt(mean_squared_error(y1_real, y2_real))
y_trfm = lambda y: np.expm1(y)
def get_score(model, X, y):
    preds = model.predict(X)
    preds = y_trfm(preds)
    y = y_trfm(y)
    return metric(preds, y)

pipeline_reg = data_preprocess_module.build_pipeline(LinearRegression())
pipeline_reg.fit(X_train, y_train)
get_score(pipeline_reg, X_val, y_val)

pipeline_lasso = data_preprocess_module.build_pipeline(Lasso())
params = {'model__alpha': [10, 1, 0.1, 0.01, 0.001]}
lasso = GridSearchCV(pipeline_lasso, params, cv=5, scoring='neg_mean_squared_error', n_jobs=-1)
time_start = time.time()
lasso.fit(X_train, y_train)
get_score(lasso, X_val, y_val)

pipeline_ridge = data_preprocess_module.build_pipeline(Ridge())
params = {'model__alpha': [10, 1, 0.1, 0.01, 0.001]}
ridge = GridSearchCV(pipeline_ridge, params, cv=5, scoring='neg_mean_squared_error', n_jobs=-1)
time_start = time.time()
ridge.fit(X_train, y_train)
print('Time taken for hyperparameter tuning: {:.2f} min'.format((time.time() - time_start) / 60))
get_score(ridge, X_val, y_val)