<jupyter_start><jupyter_text>Forecasting Air Passenger using Random Forest<jupyter_code>import numpy as np
import pandas as pd
import os
import warnings
from copy import copy
import pickle

import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV

from sklearn.ensemble import ExtraTreesRegressor

# perosnal wrapper created for time-series plots
import ts_utils
plt.rcParams['figure.figsize'] = 15, 6
plt.style.use('ggplot')
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Data preparation<jupyter_code>DATADIR = '../data/air-passenger/'
data_path = os.path.join(DATADIR, 'AirPassengers.csv')
MODELDIR = '../checkpoints/air-passenger/et/model/'
df = pd.read_csv(data_path, usecols=[1], header=0, engine='python')
data = df.values
data = data.astype('float32')
plt.plot(data)
plt.title('#Air Passengers from 1949 to 1960')
plt.ylabel('#thousands of passengers')
plt.xlabel('Years')
plt.tight_layout()
plt.show()
scaled_data = np.log(data)
print(scaled_data[:5])
plt.plot(scaled_data)
plt.title('#Air Passengers from 1949 to 1960')
plt.ylabel('Scaled #passengers')
plt.xlabel('Years')
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Train test split<jupyter_code>train, test = train_test_split(scaled_data, train_size=0.8 ,shuffle=False)
len(train), len(test)
X_train, y_train = ts_utils.prepare_data(train, time_step=1)
X_test, y_test = ts_utils.prepare_data(test, time_step=1)
print(X_train.shape) 
print(X_test.shape)<jupyter_output>(113, 1)
(27, 1)<jupyter_text>Model fitting<jupyter_code>params = {
    'max_depth': 5,
    'n_estimators': 200,
    'max_features' : 1
}
model = ExtraTreesRegressor(**params)
model.fit(X_train, y_train)
if os.path.exists(MODELDIR):
    pass
else:
    os.makedirs(MODELDIR)
pickle.dump(model, open(os.path.join(MODELDIR, 'et.model'), 'wb'))<jupyter_output><empty_output><jupyter_text>Prediction and evaluation<jupyter_code># prediction
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# the results are in the form of scaled value, so inverse the transformation
y_train_pred_inv = np.exp(y_train_pred)
y_test_pred_inv = np.exp(y_test_pred)

# will be used for calculating MAE and RMSE
y_train_inv = np.exp(y_train)
y_test_inv = np.exp(y_test)

# MAE and RMSE calculation
train_rmse = np.sqrt(mean_squared_error(y_train_inv, y_train_pred_inv))
train_mae = mean_absolute_error(y_train_inv, y_train_pred_inv)
train_nrmse = train_rmse/np.std(y_train_inv)

test_rmse = np.sqrt(mean_squared_error(y_test_inv, y_test_pred_inv))
test_mae = mean_absolute_error(y_test_inv, y_test_pred_inv)
test_nrmse = test_rmse/np.std(y_test_inv)

print(f'Training NRMSE: {train_nrmse}')
print(f'Training MAE: {train_mae}')

print(f'Test NRMSE: {test_nrmse}')
print(f'Test MAE: {test_mae}')
plt.plot(y_test_inv, label='test')
plt.plot(y_test_pred_inv, label='test predicted')
plt.xlabel('time')
plt.ylabel('#passengers')
plt.title('Actual vs predicted on test data using ExtraTrees', fontsize=14)
plt.legend()
plt.tight_layout()
plt.show()
plt.plot(y_train_inv, label='actual')
plt.plot(y_train_pred_inv, label='predicted')
plt.ylabel('#passengers')
plt.xlabel('time')
plt.title('Actual vs Predicted on Training data', fontsize=14)
plt.legend()
plt.tight_layout()
plt.show()
plt.plot(y_test_inv, label='actual')
plt.plot(y_test_pred_inv, label='predicted')
plt.ylabel('#passengers')
plt.xlabel('time')
plt.title('Actual vs Predicted on test data', fontsize=14)
plt.legend()
plt.tight_layout()
plt.show()
actual_data = np.vstack((y_train_inv, y_test_inv))
test_plot = np.empty_like(actual_data)
test_plot[:, :] = np.nan
test_plot[len(y_train_pred_inv):len(actual_data):, ] = y_test_pred_inv.reshape(-1, 1)

plt.plot(actual_data, label='actual')
plt.plot(y_train_pred_inv, color='blue', linestyle='-.', label='training prediction')
plt.plot(test_plot, color='green', linestyle='-', label='test prediction')
plt.xlabel('Years')
plt.ylabel('#thousand air passengers')
plt.title('Actual v/s Predicted using Random Forest', fontsize=15)
plt.legend()
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13470425138632164, 'fraction_numerical': 0.01155268022181146, 'mean_word_length': 7.852760736196319, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '27407', 'license': 'MIT', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1627, 'n_words': 307, 'path': '01.air_passenger/.ipynb_checkpoints/07.passenger-forecasting-ExtraTrees-checkpoint.ipynb', 'repo_name': 'mkumar73/time-series', 'size': '4328'}