Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up a baseline forecast to compare with my RNN models in Darts


How do I set up a baseline forecast to compare with my RNN models in Darts


To set up a baseline forecast to compare with your RNN models in Darts, you can use the baseline models provided by Darts. Here are the steps to follow:

1. Install Darts

- Install Darts using pip:
bash
     pip install darts
     

2. Import Libraries

- Import necessary libraries:
python
     import darts
     from darts.models import NaiveDrift, NaiveMean
     

3. Load and Preprocess Data

- Load your time series data into a pandas DataFrame:
python
     df = pd.read_csv('your_data.csv')
     

4. Create Darts Time Series

- Create Darts TimeSeries objects for the training and testing data:
python
     from darts import TimeSeries

     train_series = TimeSeries.from_dataframe(df, time_col='timestamp', value_cols='target')
     test_series = train_series.split_before(0.8)
     

5. Create Baseline Models

- Create instances of the baseline models:
python
     naive_drift_model = NaiveDrift()
     naive_mean_model = NaiveMean()
     

6. Train Baseline Models

- Train the baseline models on the training data:
python
     naive_drift_model.fit(train_series)
     naive_mean_model.fit(train_series)
     

7. Evaluate Baseline Models

- Evaluate the performance of the baseline models on the test data:
python
     naive_drift_mape = mape(naive_drift_model.predict(test_series), test_series)
     naive_mean_mape = mape(naive_mean_model.predict(test_series), test_series)

     print("Naive Drift MAPE:", naive_drift_mape)
     print("Naive Mean MAPE:", naive_mean_mape)
     

8. Compare with RNN Models

- Compare the performance of the baseline models with your RNN models:
python
     # Evaluate your RNN models
     rnn_model = RNNModel(model="LSTM", hidden_dim=20, dropout=0.2, batch_size=16, n_epochs=300)
     rnn_model.fit(train_series)

     rnn_mape = mape(rnn_model.predict(test_series), test_series)
     print("RNN Model MAPE:", rnn_mape)

     # Compare the MAPE values
     if rnn_mape < naive_drift_mape:
         print("RNN Model performed better than Naive Drift.")
     elif rnn_mape < naive_mean_mape:
         print("RNN Model performed better than Naive Mean.")
     else:
         print("Baseline models performed better.")
     

Conclusion

- By following these steps, you can set up a baseline forecast using the Naive Drift and Naive Mean models in Darts and compare their performance with your RNN models. This allows you to evaluate the effectiveness of your RNN models and identify areas for improvement.

Here is the complete code:

python
import darts
from darts.models import NaiveDrift, NaiveMean
from darts.metrics import mape

# Load and preprocess data
df = pd.read_csv('your_data.csv')
train_series = TimeSeries.from_dataframe(df, time_col='timestamp', value_cols='target')
test_series = train_series.split_before(0.8)

# Create baseline models
naive_drift_model = NaiveDrift()
naive_mean_model = NaiveMean()

# Train baseline models
naive_drift_model.fit(train_series)
naive_mean_model.fit(train_series)

# Evaluate baseline models
naive_drift_mape = mape(naive_drift_model.predict(test_series), test_series)
naive_mean_mape = mape(naive_mean_model.predict(test_series), test_series)

print("Naive Drift MAPE:", naive_drift_mape)
print("Naive Mean MAPE:", naive_mean_mape)

# Create and train RNN model
rnn_model = RNNModel(model="LSTM", hidden_dim=20, dropout=0.2, batch_size=16, n_epochs=300)
rnn_model.fit(train_series)

# Evaluate RNN model
rnn_mape = mape(rnn_model.predict(test_series), test_series)
print("RNN Model MAPE:", rnn_mape)

# Compare MAPE values
if rnn_mape < naive_drift_mape:
    print("RNN Model performed better than Naive Drift.")
elif rnn_mape < naive_mean_mape:
    print("RNN Model performed better than Naive Mean.")
else:
    print("Baseline models performed better.")

This code sets up and trains the baseline models, evaluates their performance, and compares it with your RNN model. You can adjust the hyperparameters and models to suit your specific forecasting task.

Citations:
[1] https://unit8.com/resources/training-forecasting-models/
[2] https://unit8co.github.io/darts/examples/04-RNN-examples.html
[3] https://www.kaggle.com/code/ferdinandberr/darts-forecasting-deep-learning-global-models
[4] https://unit8co.github.io/darts/generated_api/darts.models.forecasting.baselines.html
[5] https://unit8co.github.io/darts/generated_api/darts.models.forecasting.rnn_model.html