<jupyter_start><jupyter_text><NAME> - 419034  Prediction of the average price of the Spanish rail tickets data<jupyter_code>import numpy as np
import pandas as pd
import os
import time
from numpy import mean
from numpy import std
from sklearn import metrics
from sklearn.datasets import make_regression
from sklearn.model_selection import cross_val_score, train_test_split, RepeatedKFold, GridSearchCV
from sklearn.ensemble import BaggingRegressor, RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score, roc_curve, roc_auc_score, confusion_matrix
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor  
import joblib
renfe = pd.read_csv("~/Desktop/Projects/DL/Regression/Before_modeling/Datasets/renfe2.csv")
X = renfe.drop(["price"],1).astype(int) #Feature Matrix
y = renfe["price"] #Target Variable
X_train = pd.read_csv("~/Desktop/Projects/DL/Regression/Modeling/data_splits/X_train.csv")
X_test = pd.read_csv("~/Desktop/Projects/DL/Regression/Modeling/data_splits/X_test.csv")
y_train = pd.read_csv("~/Desktop/Projects/DL/Regression/Modeling/data_splits/y_train.csv")
y_test = pd.read_csv("~/Desktop/Projects/DL/Regression/Modeling/data_splits/y_test.csv")<jupyter_output><empty_output><jupyter_text>1) `Model with Cross Validation` - `Bagging`<jupyter_code>model = BaggingRegressor(n_estimators=500, random_state=123)
model.get_params()<jupyter_output><empty_output><jupyter_text>1.1) `Training` model  This model is going to be `tuned` with different `base estimators`:- `Linear regression`- `Decision tree regressor`: The same `max_depth` parameter will be used as per the one selected before- `K nearest neighbors`:  `n_neighbors` equal to `5`<jupyter_code>params = {'base_estimator': [None, LinearRegression(), DecisionTreeRegressor(max_depth=14), KNeighborsRegressor(n_neighbors= 5)]}

bagging_regressor_grid = GridSearchCV(model, param_grid =params, cv=3, n_jobs=-1)
bagging_regressor_grid.fit(X_train, y_train)

print('Best Parameters : ',bagging_regressor_grid.best_params_)<jupyter_output>/Users/Lajobu/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:73: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  return f(**kwargs)<jupyter_text>It seems the `decision tree regressor` was selected<jupyter_code>joblib.dump(bagging_regressor_grid, "bagging_regressor_grid.sav")<jupyter_output><empty_output><jupyter_text>1.2) `Prediction on the test sample`<jupyter_code>bagging_regressor_grid = joblib.load("bagging_regressor_grid.sav")
y_pred = bagging_regressor_grid.predict(X_test)
print('Mean Absolute Error', metrics.mean_absolute_error(y_test, y_pred))
print('Root Mean Squared Error', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print('R Squared Score is:', r2_score(y_test, y_pred))<jupyter_output>R Squared Score is: 0.8486368925664937<jupyter_text>1.3) `Prediction on the train sample`<jupyter_code>y_pred_ = bagging_regressor_grid.predict(X_train)
print('Mean Absolute Error', metrics.mean_absolute_error(y_train, y_pred_))
print('Root Mean Squared Error', np.sqrt(metrics.mean_squared_error(y_train, y_pred_)))
print('R Squared Score is:', r2_score(y_train, y_pred_))<jupyter_output>R Squared Score is: 0.8658482070620439<jupyter_text>1.4) `Graphical results`<jupyter_code>import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(15,10))
plot = sns.barplot(x=bagging_regressor_grid.cv_results_['param_base_estimator'].astype(str).tolist(), y=bagging_regressor_grid.cv_results_['mean_test_score'].tolist(),
            label="Total", color= "darkred")
plot.set_title("Results CV, bagging", fontsize=20, fontweight="bold")
plot.tick_params(axis='x', labelsize=13)
plot.tick_params(axis='y', labelsize=12)
fig.savefig('Figures/cv_bagging.png')
import matplotlib.pyplot as plt
fig, (ax1) = plt.subplots(figsize=(15,10))

plt.title('Actual vs predicted, bagging', fontsize=22,
            fontweight='bold')

ax1.scatter(y_test, y_pred, edgecolors=(0, 0, 0), color= "red", s=30, label='Test sample')
ax1.plot(range(350), 'k--', lw=2, color= "darkorange")
ax1.set_xlabel('Actual', fontsize=16)
ax1.set_ylabel('Predicted', fontsize=16)

ax1.scatter(y_train, y_pred_, edgecolors=(0, 0, 0), s=30,label='Training sample')

ax1.legend(loc=4, fontsize=18)
plt.show() 
fig.savefig('Figures/cv_pred_err_bagging.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12248028045574058, 'fraction_numerical': 0.025416301489921123, 'mean_word_length': 8.057539682539682, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '612665', 'license': 'MIT', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1627, 'n_words': 319, 'path': 'Modeling/Bagging/Bagging.ipynb', 'repo_name': 'lajobu/Renfe_pred_avg_price', 'size': '4575'}