text_plain_output_1.png
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

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

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'])

plt.colorbar()

california_df.isnull().sum()

# Our goal here is to extract the numeric columns so we can boxplot them in order to detect outliers.

# Numeric column extraction

numeric_columns = california_df.select_dtypes(include = ['float64', 'int']).columns
len_numeric_columns = len(numeric_columns)

# Boxplotting

fig = plt.figure(figsize = (15,10))

# Set number of columns you want to plot

n_cols = 3

n_plot_rows = len_numeric_columns//n_cols
n_plot_rows

for i, column in enumerate(numeric_columns):
    ax = fig.add_subplot(n_plot_rows, n_cols, i+1)
    sns.boxplot(y = california_df[column], orient = 'h', ax = ax)

fig.tight_layout()

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']

fig = plt.figure(figsize = (15,10))

print("Cross validation results:")
cv_results = pd.DataFrame(neigh_CV.cv_results_)
accs = pd.DataFrame(columns=["Neighbors"])

# Mostramos los resultados
melted_accs = accs.assign(**{'Neighbors': pd.DataFrame(neigh_CV.cv_results_['params']).unstack().values,
                             "Training R2": cv_results.mean_train_R2,
                             "Validation R2": cv_results.mean_test_R2,
                             "Traning MSE": cv_results.mean_train_MSE,
                             "Validation MSE": cv_results.mean_test_MSE}) \
                            .melt('Neighbors', value_vars = ['Traning MSE', 'Validation MSE'], var_name="Type", value_name="MSE")
g = sns.lineplot(x="Neighbors", y="MSE", hue='Type', data=melted_accs)

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_

df_metrics = pd.concat([pd.DataFrame(grid_search.cv_results_['params']), pd.DataFrame({'train_MSE': grid_search.cv_results_['mean_train_MSE']}), pd.DataFrame({'test_MSE': grid_search.cv_results_['mean_test_MSE']})], axis=1)
m = (df_metrics.max_features == 3) & (df_metrics.bootstrap == False)
df_metrics.loc[:, ['n_estimators', 'test_MSE']]
fig = plt.figure(figsize=(15, 10))
g = sns.lineplot(x='n_estimators', y='test_MSE', data=df_metrics, err_style=None)