Contents Menu Expand Light mode Dark mode Auto light/dark mode
NeuralProphet 1.0.0rc8 documentation
Logo
GitHub
  • Home
  • Quick Start Guide

Tutorials

  • Tutorials
    • 01: The Basics
    • 02: Trends
    • 03: Seasonality
    • 04: Auto Regression
    • 05: Lagged Regressors
    • 06: Future Regressors
    • 07: Events and Holidays
    • 08: Uncertainty
    • 09: Global Model
    • 10: Validation and Reproducibility
    • 11: Next Steps

How To Guides

  • Feature guides
    • Collect Predictions
    • Testing and Cross Validation
    • Plotting
    • Global Local Modelling
    • Uncertainty Quantification
    • Conditional Seasonality
    • Multiplicative Seasonality
    • Sparse Autoregression
    • Subdaily data
    • Hyperparameter Selection
    • MLflow Integration
    • Live Plotting during Training
    • Network Architecture Visualization
  • Application examples
    • Power Demand: Forecasting Load for a Hospital in SF
    • Renewable Energy: Forecasting Solar
    • Forecasting energy load with visualization
  • Migrate From Prophet
    • Migration from Prophet
    • Prophet to TorchProphet

Code Documentation

  • NeuralProphet
    • forecaster.py (NeuralProphet)
    • configure.py
    • time_dataset.py
    • time_net.py
    • torch_prophet.py
    • uncertainty.py
    • data/process.py
    • data/split.py
    • data/transform.py
    • components/router.py
    • components/base.py
    • components/future_regressors
    • components/seasonality
    • components/trend
    • plot_forecast_plotly.py
    • plot_forecast_matplotlib.py
    • plot_model_parameters_plotly.py
    • plot_model_parameters_matplotlib.py
    • utils.py
    • df_utils.py
    • hdays_utils.py
    • plot_utils.py
    • utils_metrics.py
    • utils_torch.py
    • custom_loss_metrics.py
    • logger.py
    • np_types.py

About

  • Model Overview
  • Presentation

Community

  • Contribute
  • GitHub
  • Slack
Back to top

Tutorial 2: Trends#

In this tutorial we will learn how to use the Trend component to model the trends of a time series.

We start with the same setup from the previous tutorial in terms of importing the necessary libraries and loading the data.

[2]:
import pandas as pd
from neuralprophet import NeuralProphet, set_log_level

# Disable logging messages unless there is an error
set_log_level("ERROR")

# Load the dataset from the CSV file using pandas
df = pd.read_csv("https://github.com/ourownstory/neuralprophet-data/raw/main/kaggle-energy/datasets/tutorial01.csv")

NeuralProphet has by default the Trend and the Seasonality components enabled. In this second tutorial we will explore the Trend component in more detail and start with its most basic configuration. Therefore we will disable the Seasonality component for now and set the number of Trend changepoints to 0 (zero).

[3]:
# Model and prediction
m = NeuralProphet(
    # Disable change trendpoints
    n_changepoints=0,
    # Disable seasonality components
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=False,
)
m.set_plotting_backend("plotly-static")
metrics = m.fit(df)
forecast = m.predict(df)
m.plot(forecast)
../_images/tutorials_tutorial02_4_3.png

We already see a linear trend line in the plot which fits well to the data.

Let us explore which trends could be seen in our dataset and how our model automatically fitted to those trends. Later we look into how to fine tune the model trend parameters.

[4]:
m.plot_components(forecast, components=["trend"])
../_images/tutorials_tutorial02_6_0.png

NeuralProphet uses a classic approach to model the trend as the combination of an offset \(m\) and a growth rate \(k\). The trend effect at a time \(t1\) is given by multiplying the growth rate \(k\) by the difference (\(t1 - t0\)) in time since the starting point \(t0\) on top of the offset \(m\).

\[\text{trend}(t1) = m + k \cdot (t1 - t0) = \text{trend}(t0) + k \cdot (t1 - t0)\]

After learning about the theory of the trend, we use the model to predict the tend into the future and see how our trend line will continue.

[5]:
df_future = m.make_future_dataframe(df, periods=365, n_historic_predictions=True)

# Predict the future
forecast = m.predict(df_future)

# Visualize the forecast
m.plot(forecast)
../_images/tutorials_tutorial02_8_1.png

The linear trend line does continue in the future. Overall there is a slight upward trend in the data.

After learning the basics of the trend in NeuralProphet, let’s look into the trend changepoints we disabled earlier. Trend changepoints are points in time where the trend changes. NeuralProphet automatically detects these changepoints and fits a new trend line to the data before and after the changepoint. Let’s see how many changepoints NeuralProphet detected.

[6]:
# Model and prediction
m = NeuralProphet(
    # Use default number of change trendpoints (10)
    # n_changepoints=0,
    # Disable seasonality components
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=False,
)
m.set_plotting_backend("matplotlib")  # Use matplotlib due to #1235
metrics = m.fit(df)
forecast = m.predict(df)
m.plot(forecast)
../_images/tutorials_tutorial02_11_3.png
[7]:
m.plot_parameters(components=["trend"])
../_images/tutorials_tutorial02_12_0.png

Now the trendline does fit the data way better. We can see that NeuralProphet used the default parameter of 10 changepoints and fit them to our data.

Next
Tutorial 3: Seasonality
Previous
Tutorial 1: The Basics
Copyright © 2024, Oskar Triebe
Made with Sphinx and @pradyunsg's Furo