<jupyter_start><jupyter_text>BLU06  - Exercise Notebook<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import warnings
idx = pd.IndexSlice
warnings.simplefilter(action='ignore', category=FutureWarning)   
from random import seed
from sklearn.metrics import mean_absolute_error
from sklearn.linear_model import LinearRegression
import numpy as np
from sklearn.metrics import mean_squared_error
import math
from sklearn.ensemble import GradientBoostingRegressor
import itertools
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa import stattools
import hashlib # for grading purposes
from sklearn.model_selection import ParameterGrid

from pandas.plotting import lag_plot
plt.rcParams['figure.figsize'] = (12, 4)

from utils import *

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Let's predict wind power production!<jupyter_code>df = pd.read_csv('data/wind_power.csv')
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
df = df.sort_index()<jupyter_output><empty_output><jupyter_text>Plot the series to get an idea of what's going on<jupyter_code>df.plot(figsize=(16, 4));
plt.ylabel('wind power')
plt.title('Wind power production for the initial months of 2010')
plt.show()<jupyter_output><empty_output><jupyter_text>Q1: Formulate it as time series one-step-ahead prediction  Q1.1 Create the target, the relevant lags and drop the missing values.<jupyter_code># Note: By relevant lags we expect you to assume the top 3 lags from the PACF (including negatively correlated ones).
# Remember from the previous BLU to look at the PACF you only need to run plot_pacf(df)

#df_features = 
#df_features['lag_a'] = 
#df_features['lag_b'] = 
#df_features['lag_c'] = 
#df_features['target'] =
#df_features = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '6e7c3f337212b8e01b67b3d63a2597b35850076095277e4cd323c07a33d02b93'
assert hashlib.sha256(str(df_features.shape).encode()).hexdigest() == expected_hash

expected_hash = 'e5351d4cf12cd01d360938a341d09da9cf5728ac1902133344147fbcab933fdc'
assert hashlib.sha256(str(df_features.iloc[0]).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q1.2 Separate the training and test set. The test set consists of the last 24 values, while the training set consists of the rest.<jupyter_code># note: this is a very straightforward question. But you may think: "isn't this one-step-ahead forecasting? 
# Why does the test have 24 values" Well, basically this just means we are doing 24 one-step-ahead forecasts.
# This way we obtain a better estimate of how our one-step-ahead model would perform in the real world.

# df_train =
# df_test =

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '4645890e7eab6608a2037bae3d98cba5a58ed02bf55ca030aa721ed1c68f7403'
assert hashlib.sha256(str(df_train.index[-1]).encode()).hexdigest() == expected_hash

expected_hash = '49a4f69687c306f42ebbc05f79ad78ab8c963cbe20e98fc11cb77629fa6b7259'
assert hashlib.sha256(str(df_test.index[0]).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q1.3 Fit a linear regression to the training set<jupyter_code># X_df_train = 
# y_df_train =
# model =
# model.fit()

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '1f7448e98bdb7d0ec574b34ef1478a0a170a96448be5ef3dfa3ba426fd2a4e9d'
assert hashlib.sha256(str(X_df_train.shape).encode()).hexdigest() == expected_hash

expected_hash = '0e39cad2ee7a31366699e40633f45b99f0211f1734d40dab9d2c5fb461ab7aa7'
assert hashlib.sha256(str(y_df_train.shape).encode()).hexdigest() == expected_hash

expected_hash = '2211e7e3e265cdb6e1337bbc7deac40ed9cd0c3203e70fded127c801e783b5f2'
assert hashlib.sha256(str(np.round(model.coef_,1)).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q1.4 Predict the test set and calculate the MAE<jupyter_code># X_df_test =
# y_df_test = 
# y_predict = 
# test_mae = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '4b259a01aeaa99e670a865121f78c1d76f029cd5158b51c72089c0bd254d17a1'
assert hashlib.sha256(str(X_df_test.shape).encode()).hexdigest() == expected_hash

expected_hash = '46ff29167351b465ba887b52b52ebddfd407ee7c8781d11225ad285541d89be5'
assert hashlib.sha256(str(y_df_test.shape).encode()).hexdigest() == expected_hash

expected_hash = '64b1c7c2dc78c85eb25fc8ebbbe7b3f27095df61f7b0621c64be58a55a042cc6'
assert hashlib.sha256(str(np.round(test_mae,5)).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q2 Let's go into multi-step prediction!  Q2.1 Separate into train, val and test. Test corresponds to the last 24 values and Val corresponds to the 24 steps before test.<jupyter_code># df_multistep_train =
# df_multistep_val =
# df_multistep_test = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = 'b9202ef2e8ac88d956f6a0fc0b5c83540a0568fc2a19b5941796f3b140d84419'
assert hashlib.sha256(str(df_multistep_train.shape).encode()).hexdigest() == expected_hash

expected_hash = '17511d2ef2dbcb5be2ccb6880d9c364c156770484a5cf8292dfe283ea2405125'
assert hashlib.sha256(str(df_multistep_val.shape).encode()).hexdigest() == expected_hash

expected_hash = '17511d2ef2dbcb5be2ccb6880d9c364c156770484a5cf8292dfe283ea2405125'
assert hashlib.sha256(str(df_multistep_test.shape).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q2.2 Let's test some hyperparameter optimization!<jupyter_code>%%time

# Create a parameter grid with the following conditions:
#    - Include the linear regression and gradient boosting regressor models. 
#      For the gradient boosting regressor use n_estimators=20 and random_state=10
#    - Test using 3 and 26 lags
#    - Test using 0 or 15 periods diffed
#    - Set the weekday, month and holidays to False. These shouldn't affect wind power.
#    - Don't use rollings.
# Use a for cycle to find the group of params that minimizes the MAE on the validation set.

# hint: to have no rollings in the predict_n_periods you should put an empty lists of lists in the param grid: [[]]


#param_grid = 

# grid = 

# for params in grid:
     # predictions =
                                    
# best_params = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '0c7f50ff83b085cd342a206e2c60d557601bddd1f378de106098f9350640d61e'
assert hashlib.sha256(str(best_params['model']).encode()).hexdigest() == expected_hash

expected_hash = '4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce'
assert hashlib.sha256(str(best_params['num_periods_lagged']).encode()).hexdigest() == expected_hash

expected_hash = '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9'
assert hashlib.sha256(str(best_params['num_periods_diffed']).encode()).hexdigest() == expected_hash

expected_hash = '1eafc175324ea36dbf3d3b7887e3887739219034fb5e3a9fb62759d6e9262b09'
assert hashlib.sha256(str(best_params).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q2.3 Train a model with the best combination and predict the test set. Calculate the corresponding MAE.<jupyter_code># We expect you to train the final model with train and val together.

# df_multistep_train_val = 
# predictions = 
# test_mae = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '389b9a7d9112bcd9a1b488224b030633d4e420fcabb1ebf9c839072046c2c4c4'
assert hashlib.sha256(str(np.round(test_mae,5)).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q3 Finally, we'll add exogenous features to improve model performance!<jupyter_code>exog = pd.read_csv('data/wind_speed_forecast.csv')
exog['date'] = pd.to_datetime(exog['date'])
exog = exog.set_index('date')
exog = exog.sort_index()<jupyter_output><empty_output><jupyter_text>Q3.1 Add the exogenous feature to the dataset and the corresponding leads<jupyter_code># Remember you want to build a number of leads corresponding to the 24 values of the next day

# df["exog"] =
# df =  

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = 'dc81d4b4ac58b278e08f7c97d999d52f0b6b2416fa6ee85c37371b65194b92e1'
assert hashlib.sha256(str(df.shape).encode()).hexdigest() == expected_hash

expected_hash = 'c958a8701a36c5c8de6af0e4be7643e2a11f603d1c7e08da1cbcc0717955b278'
assert hashlib.sha256(str(df).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q3.2 Separate into train, val and test. Test corresponds to the last 24 values and Val corresponds to the 24 steps before test.<jupyter_code># df_multistep_train =
# df_multistep_val =
# df_multistep_test = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '2837155d68248a6d1b420e270ceed5dc0a593693ad57948e1fc4df01ecf97695'
assert hashlib.sha256(str(df_multistep_train.shape).encode()).hexdigest() == expected_hash

expected_hash = '87dc6044b1bf391af8f8cd3b7bc9022b8d2a4df90c38ca4b30cae58843788eb8'
assert hashlib.sha256(str(df_multistep_val.shape).encode()).hexdigest() == expected_hash

expected_hash = '87dc6044b1bf391af8f8cd3b7bc9022b8d2a4df90c38ca4b30cae58843788eb8'
assert hashlib.sha256(str(df_multistep_test.shape).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q3.3 Let's test some hyperparameter optimisation (again)!<jupyter_code>%%time

# Create a parameter grid with the following conditions:
#    - Include the linear regression and gradient boosting regressor models. 
#      For the gradient boosting regressor use n_estimators=20 and random_state=10
#    - Test using 3 and 26 lags
#    - Set the periods diffed to 0
#    - Set the weekday, month and holidays to False. These shouldn't affect wind power.
#    - Don't use rollings.
# Use a for cycle to find the group of params that minimizes the MAE on the validation set.

# hint: This should be pretty much a copy paste from exercise 2.2. Remember that now our dataset has
#       exogenous features so we want to understand if the hyperparameter optimisation result changes.


#param_grid = 

# grid = 

# for params in grid:
     # predictions =
                                    
# best_params = 

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '4a4e3897467aca700e8b7f6bad8af525326b1299498c701bc3313b8056750bbb'
assert hashlib.sha256(str(best_params['model']).encode()).hexdigest() == expected_hash

expected_hash = '4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce'
assert hashlib.sha256(str(best_params['num_periods_lagged']).encode()).hexdigest() == expected_hash

expected_hash = '5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9'
assert hashlib.sha256(str(best_params['num_periods_diffed']).encode()).hexdigest() == expected_hash

expected_hash = '36ea53ab868bf8f53bf8cb79c37e4174c942c1c9a8fa847c8c9bb5dc338c057f'
assert hashlib.sha256(str(best_params).encode()).hexdigest() == expected_hash<jupyter_output><empty_output><jupyter_text>Q3.4 Train a model with the best combination and predict the test. Did the exogenous feature improve the MAE?<jupyter_code># We expect you to train the final model with train and val together.
# df_multistep_train_val = 
# predictions = 
# test_mae = 

# hint: Again, this should be mostly a copy paste from the exercise 2.3

# YOUR CODE HERE
raise NotImplementedError()
expected_hash = '5b0c19587a858f320eda9f4a1e31017027fb7f5a970424aa7e00b6e38eeac273'
assert hashlib.sha256(str(np.round(test_mae,5)).encode()).hexdigest() == expected_hash<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.089403399236906, 'fraction_numerical': 0.11160249739854318, 'mean_word_length': 6.883116883116883, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '506750', 'license': 'MIT', 'n_tokens_mistral': 4904, 'n_tokens_neox': 4154, 'n_words': 873, 'path': 'S03 - Time Series/BLU06 - Machine Learning for Time Series/Exercise notebook.ipynb', 'repo_name': 'FarhadManiCodes/batch5-students', 'size': '11532'}