This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATASET_NAME = 'DogsVsCats' | |
SEGMENTS = {'training_set': True, 'test_set': False} | |
def DogsVsCats(path: str) -> Dataset: | |
root_path = os.path.abspath(os.path.expanduser(path)) | |
dataset = Dataset(DATASET_NAME) | |
dataset.load_catalog(os.path.join(os.path.dirname(os.path.dirname(path)), 'catalog.json')) | |
for segment_name, is_labeled in SEGMENTS.items(): | |
segment = dataset.create_segment(segment_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Max lag order | |
max_p = 10 | |
# To store RMSE | |
errors = {} | |
for p in range(1, max_p + 1): | |
# Train and predict | |
model = AR(df_train['y']).fit(maxlag=p, dynamic=False) | |
preds = model.predict( | |
start=len(df_train), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def train_and_plot(maxlag): | |
model = AR(df_train['y']).fit(maxlag=maxlag, method='mle') | |
forecasts = model.predict( | |
start=len(df_train), | |
end=len(df_train) + len(df_test) - 1, | |
dynamic=False | |
) | |
parameters = model.params.to_dict() | |
for k, v in parameters.items(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Train/test split | |
df_train = df[:-10] | |
df_test = df[-10:] | |
# Plot | |
plt.title('Random dataset train and test sets', size=20) | |
plt.plot(df_train['y'], label='Training data') | |
plt.plot(df_test['y'], color='gray', label='Testing data') | |
plt.legend(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from sklearn.metrics import mean_squared_error | |
from statsmodels.tsa.ar_model import AR | |
import matplotlib.pyplot as plt | |
from matplotlib import rcParams | |
from cycler import cycler | |
rcParams['figure.figsize'] = 18, 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.title(f'Airline passengers Triple Exponential Smoothing predictions\nRMSE (mul, add) = {np.round(rmse_mul_add, 2)} | RMSE (mul, mul) = {np.round(rmse_mul_mul, 2)}', size=20) | |
plt.plot(df_test['Passengers'], color='gray', label='Testing data') | |
plt.plot(predictions_mul_add, color='orange', label='Predictions Multiplicative trend, Additive seasonality') | |
plt.plot(predictions_mul_mul, color='red', label='Predictions Multiplicative trend, Multiplicative seasonality') | |
plt.legend(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multiplicative trend / Additive seasonality model | |
model_mul_add = ExponentialSmoothing(df_train['Passengers'], trend='mul', seasonal='add', seasonal_periods=12) | |
results_mul_add = model_mul_add.fit() | |
predictions_mul_add = results_mul_add.forecast(steps=24) | |
# Multiplicative trend / Multiplicative seasonality model | |
model_mul_mul = ExponentialSmoothing(df_train['Passengers'], trend='mul', seasonal='mul', seasonal_periods=12) | |
results_mul_mul = model_mul_mul.fit() | |
predictions_mul_mul = results_mul_mul.forecast(steps=24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from statsmodels.tsa.holtwinters import ExponentialSmoothing | |
# Additive model | |
model_add = ExponentialSmoothing(df_train['Passengers'], trend='add') | |
results_add = model_add.fit() | |
predictions_add = results_add.forecast(steps=24) | |
# Multiplicative model | |
model_mul = ExponentialSmoothing(df_train['Passengers'], trend='mul') | |
results_mul = model_mul.fit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from statsmodels.tsa.holtwinters import SimpleExpSmoothing | |
# Model parameters | |
span = 12 | |
alpha = 2 / (span + 1) | |
# Train the model | |
model = SimpleExpSmoothing(df_train['Passengers']) | |
results = model.fit(smoothing_level=alpha, optimized=False) | |
predictions = results.forecast(steps=24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Train/test split | |
df_train = df[:-24] | |
df_test = df[-24:] | |
# Plot | |
plt.title('Airline passengers train and test sets', size=20) | |
plt.plot(df_train['Passengers'], label='Training data') | |
plt.plot(df_test['Passengers'], color='gray', label='Testing data') | |
plt.legend(); |
NewerOlder