text_plain_output_1.png
from sklearn.ensemble import GradientBoostingRegressor

from sklearn.kernel_ridge import KernelRidge

from sklearn.linear_model import LinearRegression, Lasso, ElasticNet

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import cross_val_score

from sklearn.model_selection import train_test_split

from sklearn.pipeline import make_pipeline

from sklearn.preprocessing import RobustScaler

import numpy as np

import pandas as pd

import warnings

warnings.filterwarnings('ignore')
n_jobs = -1
random_state = 42

X = pd.read_csv('/kaggle/input/modelling-ready-data/X.csv')

y = pd.read_csv('/kaggle/input/modelling-ready-data/y.csv')

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=random_state)

def rmse_cv(model):
    """
    Get Root Mean Square Error. Using KFold, split the data into 5 folds
    Finding the best score using cross_val_score()
    """
    score = cross_val_score(model, X.values, y, scoring='neg_mean_squared_error', cv=5)
    rmse = np.sqrt(-score)
    return rmse
def rmse_old(y, y_pred):
    """
    Get Root Mean Square Error without using cross_val_score
    """
    return np.sqrt(mean_squared_error(y, y_pred))

models_scores = []
def rmse(model):
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    return mean_squared_error(y_test, y_pred, squared=False)

linear_regression = make_pipeline(LinearRegression())
score = rmse(linear_regression)
models_scores.append(['LinearRegression', score])

lasso = make_pipeline(RobustScaler(), Lasso(alpha=0.0005, random_state=random_state))
score = rmse(lasso)
models_scores.append(['Lasso', score])

elastic_net = make_pipeline(RobustScaler(), ElasticNet(alpha=0.0005, l1_ratio=0.9, random_state=random_state))
score = rmse(elastic_net)
models_scores.append(['ElasticNet', score])

kernel_ridge = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5)
score = rmse(kernel_ridge)
models_scores.append(['KernelRidge', score])

models = (linear_regression, lasso, elastic_net, kernel_ridge)
for model in models:
    model.fit(X_train, y_train)
predictions = np.column_stack((model.predict(X_test) for model in models))
y_pred = np.mean(predictions, axis=1)
rmse_val = mean_squared_error(y_test, y_pred, squared=False)
models_scores.append(['Bagging', rmse_val])

gradient_boosting_regressor = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05, max_depth=4, max_features='sqrt', min_samples_leaf=15, min_samples_split=10, loss='huber', random_state=random_state)
score = rmse(gradient_boosting_regressor)
models_scores.append(['GradientBoostingRegressor', score])
print(f'GradientBoostingRegressor Score= {score}')