Few experiments Transformer based / Exponential based
Time Series Made Easy in Python
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 pandas as pd | |
from darts import TimeSeries | |
from sklearn.preprocessing import MinMaxScaler | |
import numpy as np | |
#ExponentialSmoothing Model | |
link = 'https://raw.githubusercontent.com/Geoffrey-Z/Multivariate-Time-Series-Forecasting-with-LSTMs-in-Keras-for-CORN-SWEET-Terminal-Market-Price/master/Dataset/BI-COLOR%20CORN-SWEET%20MONTHLY%20PRICE%201998-2018.csv' | |
df = pd.read_csv(link, header=0) | |
df = df.dropna() | |
print(df.head(5)) | |
import pandas as pd | |
from darts import TimeSeries | |
# Create a TimeSeries, specifying the time and value columns | |
series = TimeSeries.from_dataframe(df, 'DATE', 'PRICE') | |
# Set aside the last 36 months as a validation series | |
train, val = series[:-36], series[-36:] | |
from darts.models import ExponentialSmoothing | |
model = ExponentialSmoothing() | |
model.fit(train) | |
prediction = model.predict(len(val), num_samples=100) | |
import matplotlib.pyplot as plt | |
series.plot() | |
prediction.plot(label='forecast', low_quantile=0.05, high_quantile=0.95) | |
plt.legend() | |
#TransformerModel | |
from darts.metrics import mape | |
from darts.utils.statistics import check_seasonality, plot_acf | |
#https://unit8co.github.io/darts/examples/06-Transformer-examples.html | |
from darts.dataprocessing.transformers import Scaler | |
from darts.models import TransformerModel, ExponentialSmoothing | |
scaler = Scaler() | |
train_scaled = scaler.fit_transform(train) | |
val_scaled = scaler.transform(val) | |
series_scaled = scaler.transform(series) | |
my_model = TransformerModel( | |
input_chunk_length=12, | |
output_chunk_length=1, | |
batch_size=32, | |
n_epochs=200, | |
model_name="air_transformer", | |
nr_epochs_val_period=10, | |
d_model=16, | |
nhead=8, | |
num_encoder_layers=2, | |
num_decoder_layers=2, | |
dim_feedforward=128, | |
dropout=0.1, | |
activation="relu", | |
random_state=42, | |
save_checkpoints=True, | |
force_reset=True, | |
) | |
my_model.fit(series=train_scaled, val_series=val_scaled, verbose=True) | |
# this function evaluates a model on a given validation set for n time-steps | |
def eval_model(model, n, series, val_series): | |
pred_series = model.predict(n=n) | |
plt.figure(figsize=(8, 5)) | |
series.plot(label="actual") | |
pred_series.plot(label="forecast") | |
plt.title("MAPE: {:.2f}%".format(mape(pred_series, val_series))) | |
plt.legend() | |
eval_model(my_model, 26, series_scaled, val_scaled) |
More reads
- Past Covariates denote time series whose past values are known at prediction time.
- Future Covariates These can for instance represent known future holidays, or weather forecasts.
Multivariate-Time-series-Analysis-using-LSTM-ARIMA
Multivariate-Time-Series-Forecasting-with-LSTMs-in-Keras-for-CORN-SWEET-Terminal-Market-Price
No comments:
Post a Comment