image_output_1.png
from sklearn.ensemble import AdaBoostRegressor

from sklearn.ensemble import RandomForestRegressor

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_error, r2_score, make_scorer

from sklearn.model_selection import GridSearchCV

from sklearn.neighbors import KNeighborsRegressor

from sklearn.preprocessing import RobustScaler

from sklearn.tree import DecisionTreeRegressor

import numpy as np

import numpy as np

import pandas as pd

concat_california_array = np.concatenate((california.data, np.reshape(california.target, (california.target.shape[0], 1))), axis=1)
california_df = pd.DataFrame(concat_california_array, columns=california.feature_names + ['price'])

rs = RobustScaler()
X_train_rs = rs.fit_transform(X_train)
X_test_rs = rs.transform(X_test)

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
lg = LinearRegression()
lg.fit(X_train, y_train)
lg_rs = LinearRegression()
lg_rs.fit(X_train_rs, y_train)
y_est = lg.predict(X_test)
y_est_rs = lg_rs.predict(X_test_rs)
MSE_tst = mean_squared_error(y_test, y_est)
R2_coeff = lg.score(X_test, y_test)
MSE_tst_rs = mean_squared_error(y_test, y_est_rs)
R2_coeff_rs = lg_rs.score(X_test_rs, y_test)
MSE_train = mean_squared_error(y_train, lg.predict(X_train))
R2_train_coeff = lg.score(X_train, y_train)

from sklearn.neighbors import KNeighborsRegressor
k_max = 20
rang_K = np.arange(1, k_max + 1)
tuned_parameters = [{'n_neighbors': rang_K}]
nfold = 5
neigh_CV = GridSearchCV(KNeighborsRegressor(), tuned_parameters, cv=nfold, scoring={'MSE': make_scorer(mean_squared_error), 'R2': make_scorer(r2_score)}, return_train_score=True, refit='R2', n_jobs=-1).fit(X_train_rs, y_train)
y_est = neigh_CV.predict(X_test_rs)
MSE_tst = mean_squared_error(y_test, y_est)
R2_coeff = neigh_CV.score(X_test_rs, y_test)
K_CV = neigh_CV.best_params_['n_neighbors']

from sklearn.ensemble import RandomForestRegressor
nfold = 5
param_grid = [{'n_estimators': [3, 10, 30, 100, 150], 'max_features': [2, 4, 6, 8]}, {'bootstrap': [False], 'n_estimators': [3, 10, 30, 100], 'max_features': [2, 3, 4]}]
grid_search = GridSearchCV(RandomForestRegressor(), param_grid, cv=nfold, scoring={'MSE': make_scorer(mean_squared_error), 'R2': make_scorer(r2_score)}, return_train_score=True, refit='R2', n_jobs=-1).fit(X_train_rs, y_train)

y_est = grid_search.predict(X_test_rs)
y_est_train = grid_search.predict(X_train_rs)
MSE_tst = mean_squared_error(y_test, y_est)
R2_coeff = grid_search.score(X_test_rs, y_test)
MSE_train = mean_squared_error(y_train, y_est_train)
R2_coeff_train = grid_search.score(X_train_rs, y_train)
RF_best_params = grid_search.best_params_

from sklearn.ensemble import AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
ada_reg = AdaBoostRegressor(DecisionTreeRegressor(), n_estimators=200, learning_rate=0.5).fit(X_train_rs, y_train)
y_est = ada_reg.predict(X_test_rs)
MSE_tst = mean_squared_error(y_test, y_est)
R2_coeff = ada_reg.score(X_test_rs, y_test)
print('MSE : ' + str(MSE_tst))
print('R2 score: ' + str(R2_coeff))