# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## <NAME> - 419034

# ## Prediction of the average price of the Spanish rail tickets data 

import numpy as np
import pandas as pd
import os
import time
from sklearn import decomposition
from sklearn import metrics
from sklearn import tree
from sklearn.pipeline import Pipeline
from sklearn.tree import DecisionTreeRegressor  
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.metrics import r2_score, roc_curve, roc_auc_score, confusion_matrix
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import joblib

renfe = pd.read_csv("~/Desktop/Projects/DL/Regression/Before_modeling/Datasets/renfe2.csv")

# # 0) `Spliting the data`

# ### In this first model the data will be splitted as per below:
# - `20%` - `test data`
# - `80%` - `train data`

# ### `X` are the regressors, hence the following `9 features`:
# - `origin`
# - `destination`
# - `duration`
# - `vehicle_type`
# - `fare`
# - `week_day`
# - `month`
# - `time`
# - `distance`

# ### `y` is the target variable, `price`

X = renfe.drop(["price"],1).astype(int) #Feature Matrix
y = renfe["price"] #Target Variable

# +
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# +
#X_train.to_csv("~/Desktop/Projects/DL/Regression/X_train.csv", index=False)
#X_test.to_csv("~/Desktop/Projects/DL/Regression/X_test.csv", index=False)
#y_train.to_csv("~/Desktop/Projects/DL/Regression/y_train.csv", index=False)
#y_test.to_csv("~/Desktop/Projects/DL/Regression/y_test.csv", index=False)
# -

# ### These above `explits` are going to be `used in the rest of the models`

# # 1) `Model without Cross Validation` - `Decision trees`

# ### Firstly, a model `without a cross validation` will be run. In such way it will be possible to see a `representation of the decision tree`, which will have the `maximum depth` assigned equal to `3`

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")

clf_ = tree.DecisionTreeRegressor(max_depth=3, random_state=123)
clf_.fit(X_train, y_train) 

clf_.get_params()

fig = plt.figure(figsize=(20,12))
_ = tree.plot_tree(clf_, 
                   feature_names= renfe.columns.tolist()[:9],  
                   class_names= ["price"],
                   filled=True)
plt.title('Decision tree',
            fontsize=22,
            fontweight='bold')
plt.savefig('Figures/dec_tree.png')

# ### Above a representation of the `decision tree` can be seen 

# # 2) `Model with Cross Validation` - `Decision trees`

std_slc = StandardScaler()
pca = decomposition.PCA()
dtreeReg = tree.DecisionTreeRegressor(random_state=123)

dtreeReg.get_params()

# ## 2.1) `Training` model

# ### This model is going to be `tuned` with different parameters of `max depth`, [2, 4, 6 ... 18]

# +
params = {'max_depth': [2, 4, 6, 8, 10, 12, 14, 16, 18]}

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

print('Best Parameters : ',tree_regressor_grid.best_params_)
# -

# ### It seems the best model has the `max depth` equal to `14`

joblib.dump(tree_regressor_grid, "tree_regressor_grid.sav")

# ## 2.2) `Prediction on the test sample`

tree_regressor_grid = joblib.load("tree_regressor_grid.sav")

renfe.columns

y_pred = tree_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))

# ## 2.3) `Prediction on the train sample`

y_pred_ = tree_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_))

# ## 2.4) `Graphical results`

import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8,8))
plt.plot(tree_regressor_grid.cv_results_['param_max_depth'].tolist(), tree_regressor_grid.cv_results_['mean_test_score'].tolist(), lw=8, color= "darkred")
ax.set_title("Results CV, decision tree", fontsize=20, fontweight="bold")
ax.tick_params(axis='x', labelsize=13)
ax.tick_params(axis='y', labelsize=12)
plt.xlabel('Max depth', fontsize=13, fontweight="bold")
plt.ylabel('Mean test score', fontsize=13, fontweight="bold")
fig.savefig('Figures/cv_dec_tree.png')
plt.show()

# +
import matplotlib.pyplot as plt
fig, (ax1) = plt.subplots(figsize=(15,10))

plt.title('Actual vs predicted, decision tree', 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_tree.png')
# -

tree_regressor_grid.predict(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(1, -1)) # to make a predition

# # 3) `Conclusions`

# ### The best parameter for the model is `max depth` equal to `14`
# ### It seems there is `not a lot of difference` between the `R squared score` of the `test` and `training` samples
# ### In order to compare the models  `R squared score of the test sample` will be taken, which is equal to `83.93%`

StarcoderdataJupyterScriptsDedupFiltered