### Running NeuralProphet Development Setup Script and Git Configuration - Shell Source: https://github.com/ourownstory/neural_prophet/blob/main/CONTRIBUTING.md This snippet shows how to execute the `neuralprophet_dev_setup.py` script to install pre-commit and pre-push git hooks (for Black and PyTest) and configure git to use fast-forward only pulls, preventing accidental merges. ```Shell neuralprophet_dev_setup.py git config pull.ff only ``` -------------------------------- ### Installing Network Visualization Dependencies (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet attempts to import `torchsummary` and `torchviz`. If unsuccessful, it installs `graphviz` based on the operating system (Mac/Linux) and then installs Python packages `torchsummary`, `torch-summary`, `torchviz`, and `graphviz` using pip. This ensures the necessary libraries for model visualization are available. ```python try: # it already installed dependencies from torchsummary import summary from torchviz import make_dot except ImportError: # install graphviz on system import platform if "Darwin" == platform.system(): !brew install graphviz elif "Linux" == platform.system(): !sudo apt install graphviz else: print("go to https://www.graphviz.org/download/") # Next we need to install the following dependencies: !pip install torchsummary !pip install torch-summary !pip install torchviz !pip install graphviz # import from torchsummary import summary from torchviz import make_dot ``` -------------------------------- ### Installing NeuralProphet and Importing Libraries (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/season_multiplicative_air_travel.ipynb Installs NeuralProphet from GitHub and imports necessary libraries like pandas and NeuralProphet, setting the log level to suppress verbose output. This setup is crucial for running the forecasting examples. ```python if "google.colab" in str(get_ipython()): # uninstall preinstalled packages from Colab to avoid conflicts !pip uninstall -y torch notebook notebook_shim tensorflow tensorflow-datasets prophet torchaudio torchdata torchtext torchvision !pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes import pandas as pd from neuralprophet import NeuralProphet, set_log_level set_log_level("ERROR") ``` -------------------------------- ### Setting Up NeuralProphet in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/collect_predictions.ipynb This snippet handles the initial setup for running NeuralProphet, including conditional uninstallation of conflicting packages in Google Colab environments and installing NeuralProphet. It then imports necessary libraries like pandas and NeuralProphet, and sets the logging level to suppress verbose output. ```python if "google.colab" in str(get_ipython()): # uninstall preinstalled packages from Colab to avoid conflicts !pip uninstall -y torch notebook notebook_shim tensorflow tensorflow-datasets prophet torchaudio torchdata torchtext torchvision !pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes import pandas as pd from neuralprophet import NeuralProphet, set_log_level set_log_level("ERROR") ``` -------------------------------- ### Training NeuralProphet Model - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This code trains the NeuralProphet model 'm' using training data 'df_train' and validates it against 'df_test'. It specifies a frequency of 'h' (hourly) and disables early stopping. The commented-out sections show examples of configuring learning rate schedulers. ```Python metrics = m.fit( df=df_train, validation_df=df_test, freq="h", early_stopping=False, # scheduler="onecyclelr", # scheduler_args={ # "pct_start": 0.3, # "div_factor": 100.0, # "final_div_factor": 1000.0, # "anneal_strategy": "cos", # "three_phase": False, # }, # scheduler="exponentiallr", # scheduler_args={"gamma": 0.8,}, ) ``` -------------------------------- ### Installing NeuralProphet Library (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet attempts to import `NeuralProphet`. If the import fails, it installs the `neuralprophet` library directly from its GitHub repository using pip, ensuring the core forecasting library is available for use. ```python try: from neuralprophet import NeuralProphet except ImportError: # if NeuralProphet is not installed yet: !pip install git+https://github.com/ourownstory/neural_prophet.git from neuralprophet import NeuralProphet ``` -------------------------------- ### Installing NeuralProphet from GitHub (Shell) Source: https://github.com/ourownstory/neural_prophet/blob/main/README.md Clones the NeuralProphet repository from GitHub, navigates into the project directory, and installs the package in editable mode, providing the most up-to-date version directly from the source. ```shell git clone cd neural_prophet pip install . ``` -------------------------------- ### Defining Data File Paths for NeuralProphet Examples (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb This snippet defines common directory and file paths for various test datasets used in NeuralProphet examples, including Peyton Manning, Air Passengers, Yosemite temperatures, and daily energy prices. These paths are constructed using `os.path.join` for cross-platform compatibility. ```python DIR = "~/github/neural_prophet" DATA_DIR = os.path.join(DIR, "tests", "test-data") PEYTON_FILE = os.path.join(DATA_DIR, "wp_log_peyton_manning.csv") AIR_FILE = os.path.join(DATA_DIR, "air_passengers.csv") YOS_FILE = os.path.join(DATA_DIR, "yosemite_temps.csv") ENERGY_PRICE_DAILY_FILE = os.path.join(DATA_DIR, "tutorial04_kaggle_energy_daily_temperature.csv") ``` -------------------------------- ### Installing NeuralProphet in Development Mode with Pip - Shell Source: https://github.com/ourownstory/neural_prophet/blob/main/CONTRIBUTING.md This snippet provides shell commands for cloning the NeuralProphet repository and installing it in 'editable' development mode using pip. The `.[dev]` extra installs additional development dependencies required for contributing. ```Shell git clone cd neural_prophet pip install -e ".[dev]" ``` -------------------------------- ### Installing NeuralProphet via Pip Source: https://github.com/ourownstory/neural_prophet/blob/main/README.md This command-line snippet provides the standard method for installing the NeuralProphet library using Python's package installer, pip. This makes the library available for use in any Python environment. ```shell pip install neuralprophet ``` -------------------------------- ### Installing MLflow and NeuralProphet Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This snippet provides instructions for installing MLflow and NeuralProphet. It includes conditional uninstallation of conflicting packages for Google Colab environments and options for installing NeuralProphet from source or via pip. ```python # for this tutorial, we need to install MLflow. # !pip install mlflow # Start a MLflow tracking-server on your local machine # !mlflow server --host 127.0.0.1 --port 8080 if "google.colab" in str(get_ipython()): # uninstall preinstalled packages from Colab to avoid conflicts !pip uninstall -y torch notebook notebook_shim tensorflow tensorflow-datasets prophet torchaudio torchdata torchtext torchvision !pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while # much faster using the following code, but may not have the latest upgrades/bugfixes # pip install neuralprophet ``` -------------------------------- ### Manually Running Tests and Code Formatting - Shell Source: https://github.com/ourownstory/neural_prophet/blob/main/CONTRIBUTING.md This snippet provides commands for manually running PyTest for testing and Black for code formatting, useful when git hooks are not installed or fail. It also includes an alternative command for running the dev setup script. ```Shell pytest -v python3 -m black {source_file_or_directory} python ./scripts/neuralprophet_dev_setup.py ``` -------------------------------- ### Installing NeuralProphet for Live Plotting (Shell) Source: https://github.com/ourownstory/neural_prophet/blob/main/README.md Installs the 'live' version of NeuralProphet, enabling the `plot_live_loss` feature in Jupyter notebooks for real-time visualization of training and validation loss during model fitting. ```shell pip install neuralprophet[live] ``` -------------------------------- ### Loading Example Time Series Data with Pandas Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/Migration_from_Prophet.ipynb This snippet loads a sample time series dataset from a GitHub URL into a pandas DataFrame. This DataFrame, `df`, is used as input for both Prophet and NeuralProphet models throughout the migration examples. ```Python df = pd.read_csv("https://raw.githubusercontent.com/facebook/prophet/main/examples/example_wp_log_peyton_manning.csv") ``` -------------------------------- ### Plotting NeuralProphet Forecast - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This code first highlights a specific step ahead in each forecast (determined by 'n_forecasts') and then plots the generated 'forecast' against the 'test' dataset. This visualization helps in assessing the model's predictive accuracy. ```Python m.highlight_nth_step_ahead_of_each_forecast(m.config_model.n_forecasts) m.plot(forecast, df_name="test") ``` -------------------------------- ### Importing Libraries and Setting Log Level in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet imports essential libraries for data manipulation (pandas, numpy), plotting (plotly), and the core NeuralProphet library. It also sets the logging level for NeuralProphet to 'INFO' for verbose output. ```Python import os import numpy as np import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots from plotly_resampler import unregister_plotly_resampler from neuralprophet import NeuralProphet, set_log_level set_log_level("INFO") ``` -------------------------------- ### Setting up Development Environment with Poetry - Shell Source: https://github.com/ourownstory/neural_prophet/blob/main/CONTRIBUTING.md This snippet provides a sequence of shell commands to set up the NeuralProphet development environment using Poetry. It covers cloning the repository, creating and activating a Poetry virtual environment, and installing project dependencies in editable mode. It also includes commands for updating dependencies. ```Shell git clone cd neural_prophet poetry shell poetry env info --path poetry install poetry update ``` -------------------------------- ### Displaying Training Metrics Object - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This simple line displays the 'metrics' object itself, which typically provides a summary or representation of the training and validation performance data. ```Python metrics ``` -------------------------------- ### Creating and Activating a Virtual Environment - Shell Source: https://github.com/ourownstory/neural_prophet/blob/main/CONTRIBUTING.md This snippet demonstrates how to create and activate a new Python virtual environment using `venv` before installing NeuralProphet. This ensures project dependencies are isolated from the system Python installation. ```Shell python3 -m venv source /bin/activate ``` -------------------------------- ### MLflow UI Command Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This snippet provides a commented-out command to start the MLflow UI server. It serves as a reminder for users to run this command in their environment to access the MLflow web interface and view experiment results. ```python # MLflow setup # Run this command with environment activated: mlflow ui --port xxxx (e.g. 5000, 5001, 5002) ``` -------------------------------- ### Defining Data Directory and File Paths in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet defines the base directory for the NeuralProphet project and constructs full file paths for various test datasets, including 'wp_log_peyton_manning.csv', 'air_passengers.csv', 'yosemite_temps.csv', and 'tutorial04_kaggle_energy_daily_temperature.csv'. These paths are used for loading data in subsequent steps. ```Python DIR = "~/github/neural_prophet" DATA_DIR = os.path.join(DIR, "tests", "test-data") PEYTON_FILE = os.path.join(DATA_DIR, "wp_log_peyton_manning.csv") AIR_FILE = os.path.join(DATA_DIR, "air_passengers.csv") YOS_FILE = os.path.join(DATA_DIR, "yosemite_temps.csv") ENERGY_PRICE_DAILY_FILE = os.path.join(DATA_DIR, "tutorial04_kaggle_energy_daily_temperature.csv") ``` -------------------------------- ### Plotting Training Metrics - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet visualizes the training and validation metrics obtained from the model fitting process. It uses a utility function 'create_metrics_plot' to generate a plot based on the 'metrics' object. ```Python create_metrics_plot(metrics) ``` -------------------------------- ### Plotting NeuralProphet Forecast Components - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet visualizes the individual components of the forecast, such as trend, seasonality, and regressors. This helps in understanding the contribution of each component to the overall prediction. ```Python m.plot_components(forecast, df_name="test") ``` -------------------------------- ### Installing NeuralProphet and Importing Libraries (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/conditional_seasonality_peyton.ipynb This snippet handles the installation of NeuralProphet, specifically for Google Colab environments, by uninstalling conflicting packages and then installing NeuralProphet from a specific GitHub branch. It also imports necessary libraries like pandas and NeuralProphet components, and sets the logging level to suppress verbose output. ```Python if "google.colab" in str(get_ipython()): # uninstall preinstalled packages from Colab to avoid conflicts !pip uninstall -y torch notebook notebook_shim tensorflow tensorflow-datasets prophet torchaudio torchdata torchtext torchvision !pip install git+https://github.com/ourownstory/neural_prophet.git@examples_cond_seas # may take a while #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes import pandas as pd from neuralprophet import NeuralProphet, set_log_level, df_utils set_log_level("ERROR") ``` -------------------------------- ### Plotting NeuralProphet Model Parameters (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Generates plots of the learned parameters of the `NeuralProphet` model, such as changepoints, seasonality curves, and regressor coefficients. This provides insights into how the model has captured patterns in the data. ```python m.plot_parameters() ``` -------------------------------- ### Plotting NeuralProphet Model Parameters - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This code generates plots of the learned parameters of the NeuralProphet model. This can include parameters for trend, seasonality, and regressors, providing insights into the model's internal structure. ```Python m.plot_parameters() ``` -------------------------------- ### Initializing NeuralProphet Model and Adding Components - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet initializes a NeuralProphet model with specified parameters and then configures it by adding a lagged regressor for 'temp', two conditional seasonalities ('winter' and 'summer'), and country-specific holidays for the US. These configurations prepare the model for training by incorporating relevant time series features. ```Python m = NeuralProphet(**tuned_params, **trainer_configs, quantiles=quantile_list) m.add_lagged_regressor(names="temp", n_lags=33, normalize="standardize") m.add_seasonality(name="winter", period=1, fourier_order=6, condition_name="winter") m.add_seasonality(name="summer", period=1, fourier_order=6, condition_name="summer") m.add_country_holidays(country_name="US", lower_window=-1, upper_window=1) ``` -------------------------------- ### Generating Forecast with NeuralProphet - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet uses the trained NeuralProphet model 'm' to generate a forecast on the provided dataframe 'df'. The 'predict' method outputs future predictions based on the model's learned patterns. ```Python forecast = m.predict(df) ``` -------------------------------- ### Basic NeuralProphet Usage Example (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/contents.rst This snippet illustrates the fundamental steps to use NeuralProphet for time series forecasting. It covers importing the NeuralProphet class, initializing a model instance, fitting the model to a DataFrame, generating predictions, and visualizing the forecast. The input `df` is expected to be a pandas DataFrame containing time series data. ```Python >>> from neuralprophet import NeuralProphet >>> m = NeuralProphet() >>> metrics = m.fit(df) >>> forecast = m.predict(df) >>> m.plot(forecast) ``` -------------------------------- ### Plotting NeuralProphet Training Metrics (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Calls the previously defined `create_metrics_plot` function to visualize the training and validation metrics obtained from fitting the `NeuralProphet` model. This helps in assessing the model's learning progress and identifying potential overfitting. ```python create_metrics_plot(metrics) ``` -------------------------------- ### Importing NeuralProphet and Plotting Libraries (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb This snippet imports necessary libraries for time series forecasting with NeuralProphet, data manipulation with pandas, and plotting with plotly. It also includes `unregister_plotly_resampler` for compatibility with image export. ```python import os import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots from plotly_resampler import unregister_plotly_resampler from neuralprophet import NeuralProphet ``` -------------------------------- ### Plotting NeuralProphet Forecast Components (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Visualizes the individual components of the `NeuralProphet` forecast, such as trend, seasonality (yearly, weekly, daily), and regressor effects. This helps in understanding the contribution of each component to the final prediction. ```python m.plot_components(forecast) ``` -------------------------------- ### In-Sample Predictions by Forecast Start (Raw) in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/collect_predictions.ipynb This code demonstrates using `predict` with `raw=True` and `decompose=False` to obtain in-sample predictions. In this 'raw' format, columns like `step` refer to the `i`th step-ahead prediction starting from the current row's datetime, meaning all predictions in a row originate from the same forecast start point. ```python df = pd.read_csv(data_location + "air_passengers.csv") forecast = m.predict(df, decompose=False, raw=True) forecast.tail(3) ``` -------------------------------- ### Initializing NeuralProphet Model with Regressors (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Initializes a `NeuralProphet` model instance, configuring it for 7-step-ahead forecasts, no changepoints, and enabling yearly and weekly seasonality. It also adds 'temp' as a lagged regressor with 3 lags and 'temperature' as a future regressor, indicating external variables influencing the forecast. ```python m = NeuralProphet( n_forecasts=7, n_changepoints=0, yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=False, n_lags=14, ) m.add_lagged_regressor("temp", n_lags=3) m.add_future_regressor("temperature") ``` -------------------------------- ### Loading and Preparing Energy Price Data for NeuralProphet (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Loads the daily energy price dataset from a CSV file into a pandas DataFrame. It then renames the 'temperature' column to 'temp' for consistency or specific model input requirements. ```python df = pd.read_csv(ENERGY_PRICE_DAILY_FILE) df["temp"] = df["temperature"] ``` -------------------------------- ### Splitting Data and Fitting NeuralProphet Model (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Splits the prepared DataFrame `df` into training and validation sets using a daily frequency and 10% for validation. It then fits the `NeuralProphet` model `m` to the training data, using the validation set to monitor performance during training, and stores the training metrics. ```python df_train, df_test = m.split_df(df=df, freq="D", valid_p=0.1) metrics = m.fit(df_train, validation_df=df_test, freq="D") ``` -------------------------------- ### Configuring Local MLflow Tracking Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This snippet defines a boolean variable `local` to control whether the MLflow tracking server is configured for a local setup. Setting `local` to `True` enables local tracking. ```python # Set variable 'local' to True if you want to run this notebook locally local = False ``` -------------------------------- ### Generating Forecasts with NeuralProphet (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Uses the trained `NeuralProphet` model `m` to generate future forecasts based on the entire input DataFrame `df`. The `predict` method produces a DataFrame containing the forecasted values and components. ```python forecast = m.predict(df) ``` -------------------------------- ### Generating Out-of-Sample Forecasts by Start (Raw) in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/collect_predictions.ipynb This snippet shows how to obtain out-of-sample forecasts using the `predict` method with `raw=True` and `decompose=False` on the `future` DataFrame. In this format, `stepX` columns represent predictions X steps ahead from the forecast's starting datetime, providing a different perspective on the future predictions. ```python forecast = m.predict(future, raw=True, decompose=False) forecast ``` -------------------------------- ### Plotting NeuralProphet Forecast (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Highlights the 7th step ahead of each forecast in the generated `forecast` DataFrame, then visualizes the overall forecast using the model's built-in plotting function. This plot typically shows the actual values, the forecast, and confidence intervals. ```python m.highlight_nth_step_ahead_of_each_forecast(7) m.plot(forecast) ``` -------------------------------- ### Accessing Last Training Metric Record - Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This code retrieves the last record from the 'metrics' object, converting it to a dictionary format. This is useful for quickly inspecting the final training and validation performance metrics. ```Python metrics.to_dict("records")[-1] ``` -------------------------------- ### Configuring NeuralProphet Hyperparameters and Trainer Settings Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet defines a dictionary of hyperparameters ('tuned_params') for the NeuralProphet model, including settings for lags, seasonality, batch size, and neural network layers. It also configures uncertainty quantification by defining 'confidence_lv' and 'quantile_list', and sets up trainer configurations ('trainer_configs') to specify the accelerator (CPU or GPU) based on availability. These parameters are crucial for model training. ```Python # Hyperparameter tuned_params = { "n_lags": 10, "newer_samples_weight": 2.0, "n_changepoints": 0, "yearly_seasonality": 10, "weekly_seasonality": True, "daily_seasonality": False, # due to conditional daily seasonality "batch_size": 32, "ar_layers": [8, 4], "lagged_reg_layers": [8], # not tuned "n_forecasts": 5, # "learning_rate": 0.1, "epochs": 10, "trend_global_local": "global", "season_global_local": "global", "drop_missing": True, "normalize": "standardize", } # Uncertainty Quantification confidence_lv = 0.98 quantile_list = [round(((1 - confidence_lv) / 2), 2), round((confidence_lv + (1 - confidence_lv) / 2), 2)] # quantile_list = None print(f"quantiles: {quantile_list}") # Check if GPU is available # use_gpu = torch.cuda.is_available() use_gpu = False # Set trainer configuration trainer_configs = { "accelerator": "gpu" if use_gpu else "cpu", } print(f"Using {'GPU' if use_gpu else 'CPU'}") ``` -------------------------------- ### Loading Air Passengers Dataset (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/season_multiplicative_air_travel.ipynb Loads the air passengers time series data from a specified URL into a pandas DataFrame. This dataset serves as the example for demonstrating both additive and multiplicative seasonality. ```python data_location = "https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/" df = pd.read_csv(data_location + "air_passengers.csv") ``` -------------------------------- ### Starting MLflow Experiment with NeuralProphet Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This comprehensive snippet demonstrates how to initiate an MLflow run, create an experiment, log hyperparameters, train a NeuralProphet model, log training metrics (MAE, RMSE, Loss, duration), save the model, and log it as an MLflow artifact. The entire process is conditional on the `local` variable. ```python if local: with mlflow.start_run(): # Create a new MLflow experiment mlflow.set_experiment("NP-MLflow Quickstart_v1") # Set a tag for the experiment mlflow.set_tag("Description", "NeuralProphet MLflow Quickstart") # Define NeuralProphet hyperparameters params = { "n_lags": 5, "n_forecasts": 3, } # Log Hyperparameters mlflow.log_params(params) # Initialize NeuralProphet model and fit start = time.time() m = NeuralProphet(**params) metrics_train = m.fit(df=df, freq="MS") end = time.time() # Log training duration mlflow.log_metric("duration", end - start) # Log training metrics mlflow.log_metric("MAE_train", value=list(metrics_train["MAE"])[-1]) mlflow.log_metric("RMSE_train", value=list(metrics_train["RMSE"])[-1]) mlflow.log_metric("Loss_train", value=list(metrics_train["Loss"])[-1]) # save model model_path = "np-model.np" save(m, model_path) # Log the model in MLflow mlflow.log_artifact(model_path, "np-model") ``` -------------------------------- ### Fitting NeuralProphet Model in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/collect_predictions.ipynb This snippet initializes a NeuralProphet model instance, configuring it with 5 lags and 3 forecast steps. It then fits the model to the loaded DataFrame (`df`), specifying a monthly start frequency ('MS') for the time series data. The training metrics are stored in `metrics_train`. ```python m = NeuralProphet(n_lags=5, n_forecasts=3) metrics_train = m.fit(df=df, freq="MS") ``` -------------------------------- ### Starting MLflow Run and Logging Data/Parameters in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This code block initiates an MLflow run, logs a Pandas DataFrame as a dataset with a 'training' context, and records various model parameters such as `model_type`, `n_lags`, `ar_layers`, and `accelerator` for experiment tracking. This ensures all relevant inputs and configurations are captured with the run. ```python with mlflow.start_run() as run: dataset: PandasDataset = mlflow.data.from_pandas(df, source="AirPassengersDataset") # Log the dataset to the MLflow Run. Specify the "training" context to indicate that the # dataset is used for model training mlflow.log_input(dataset, context="training") mlflow.log_param("model_type", "NeuralProphet") mlflow.log_param("n_lags", 8) mlflow.log_param("ar_layers", [8, 8, 8, 8]) mlflow.log_param("accelerator", "gpu") ``` -------------------------------- ### In-Sample Predictions by Forecast Start (Raw, Decomposed) in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/collect_predictions.ipynb This snippet is similar to the previous one but sets `decompose=True` along with `raw=True`. This allows the output to include individual forecast components for each `step` prediction, providing a more detailed breakdown of the forecast contributions. ```python df = pd.read_csv(data_location + "air_passengers.csv") forecast = m.predict(df, decompose=True, raw=True) forecast.tail(3) ``` -------------------------------- ### Ending Previous MLflow Run Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This snippet provides a commented-out line to explicitly end any previously active MLflow runs. This is a precautionary measure to prevent errors if a run is already active before starting a new experiment. ```python # End previous run if any # mlflow.end_run() ``` -------------------------------- ### Creating Metrics Plot Function for NeuralProphet (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Defines a utility function `create_metrics_plot` to visualize training and validation metrics from a NeuralProphet model. It configures Plotly parameters for line plots, handles multiple metrics, and includes options for validation and regularization loss, ensuring compatibility with image export by deactivating `plotly_resampler`. ```python def create_metrics_plot(metrics): # Deactivate the resampler since it is not compatible with kaleido (image export) unregister_plotly_resampler() # Plotly params prediction_color = "#2d92ff" actual_color = "black" line_width = 2 xaxis_args = {"showline": True, "mirror": True, "linewidth": 1.5, "showgrid": False} yaxis_args = { "showline": True, "mirror": True, "linewidth": 1.5, "showgrid": False, "rangemode": "tozero", "type": "log", } layout_args = { "autosize": True, "template": "plotly_white", "margin": go.layout.Margin(l=0, r=10, b=0, t=30, pad=0), "font": dict(size=10), "title": dict(font=dict(size=10)), "width": 1000, "height": 200, } metric_cols = [col for col in metrics.columns if not ("_val" in col or col == "RegLoss" or col == "epoch")] fig = make_subplots(rows=1, cols=len(metric_cols), subplot_titles=metric_cols) for i, metric in enumerate(metric_cols): fig.add_trace( go.Scatter( y=metrics[metric], name=metric, mode="lines", line=dict(color=prediction_color, width=line_width), legendgroup=metric, ), row=1, col=i + 1, ) if f"{metric}_val" in metrics.columns: fig.add_trace( go.Scatter( y=metrics[f"{metric}_val"], name=f"{metric}_val", mode="lines", line=dict(color=actual_color, width=line_width), legendgroup=metric, ), row=1, col=i + 1, ) if metric == "Loss": fig.add_trace( go.Scatter( y=metrics["RegLoss"], name="RegLoss", mode="lines", line=dict(color=actual_color, width=line_width), legendgroup=metric, ), row=1, col=i + 1, ) fig.update_xaxes(xaxis_args) fig.update_yaxes(yaxis_args) fig.update_layout(layout_args) return fig ``` -------------------------------- ### Forecasting Future Data with NeuralProphet in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/README.md This example demonstrates how to perform out-of-sample forecasting into the future. It involves fitting the model with a specified frequency, creating a future DataFrame for a given number of periods, predicting values for this future period, and then plotting the results. ```python m = NeuralProphet().fit(df, freq="D") df_future = m.make_future_dataframe(df, periods=30) forecast = m.predict(df_future) fig_forecast = m.plot(forecast) ``` -------------------------------- ### Accessing Last Training Metrics Record (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Retrieves the last record from the `metrics` DataFrame, which typically contains the final training and validation loss values and other metrics after the model fitting process. It converts the DataFrame to a list of dictionaries and selects the last element. ```python metrics.to_dict("records")[-1] ``` -------------------------------- ### Creating Plotly Metrics Plot for NeuralProphet in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This function generates a Plotly figure to visualize training metrics. It deactivates plotly_resampler for image export compatibility, defines plotting parameters, iterates through metric columns to add traces for training and validation metrics, and updates the layout before returning the figure. It expects a pandas DataFrame 'metrics' as input. ```Python def create_metrics_plot(metrics): # Deactivate the resampler since it is not compatible with kaleido (image export) unregister_plotly_resampler() # Plotly params prediction_color = "#2d92ff" actual_color = "black" line_width = 2 xaxis_args = {"showline": True, "mirror": True, "linewidth": 1.5, "showgrid": False} yaxis_args = { "showline": True, "mirror": True, "linewidth": 1.5, "showgrid": False, "rangemode": "tozero", "type": "log", } layout_args = { "autosize": True, "template": "plotly_white", "margin": go.layout.Margin(l=0, r=10, b=0, t=30, pad=0), "font": dict(size=10), "title": dict(font=dict(size=10)), "width": 1000, "height": 200, } metric_cols = [col for col in metrics.columns if not ("_val" in col or col == "RegLoss" or col == "epoch")] fig = make_subplots(rows=1, cols=len(metric_cols), subplot_titles=metric_cols) for i, metric in enumerate(metric_cols): fig.add_trace( go.Scatter( y=metrics[metric], name=metric, mode="lines", line=dict(color=prediction_color, width=line_width), legendgroup=metric, ), row=1, col=i + 1, ) if f"{metric}_val" in metrics.columns: fig.add_trace( go.Scatter( y=metrics[f"{metric}_val"], name=f"{metric}_val", mode="lines", line=dict(color=actual_color, width=line_width), legendgroup=metric, ), row=1, col=i + 1, ) if metric == "Loss": fig.add_trace( go.Scatter( y=metrics["RegLoss"], name="RegLoss", mode="lines", line=dict(color=actual_color, width=line_width), legendgroup=metric, ), row=1, col=i + 1, ) fig.update_xaxes(xaxis_args) fig.update_yaxes(yaxis_args) fig.update_layout(layout_args) return fig ``` -------------------------------- ### Loading and Preprocessing Time Series Data with Pandas Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-hourly.ipynb This snippet loads daily energy price data, renames a column, converts the 'ds' column to datetime objects, and ensures 'y' is numeric. It then generates a new 'ds' column with hourly frequency, adds an 'ID' column, and concatenates a modified copy of the data to create a multi-ID dataset. Finally, it adds conditional seasonality features ('winter', 'summer'), normalizes the 'temp' column, reorders columns, and splits the data into training and testing sets based on a date threshold. ```Python df = pd.read_csv(ENERGY_PRICE_DAILY_FILE) df["temp"] = df["temperature"] df = df.drop(columns="temperature") df["ds"] = pd.to_datetime(df["ds"]) df["y"] = pd.to_numeric(df["y"], errors="coerce") df = df.drop("ds", axis=1) df["ds"] = pd.date_range(start="2015-01-01 00:00:00", periods=len(df), freq="h") df["ID"] = "test" df_id = df[["ds", "y", "temp"]].copy() df_id["ID"] = "test2" df_id["y"] = df_id["y"] * 0.3 df_id["temp"] = df_id["temp"] * 0.4 df = pd.concat([df, df_id], ignore_index=True) # Conditional Seasonality df["winter"] = np.where( df["ds"].dt.month.isin([1]), 1, 0, ) df["summer"] = np.where(df["ds"].dt.month.isin([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), 1, 0) df["winter"] = pd.to_numeric(df["winter"], errors="coerce") df["summer"] = pd.to_numeric(df["summer"], errors="coerce") # Normalize Temperature df["temp"] = (df["temp"] - 65.0) / 50.0 # df df = df[["ID", "ds", "y", "temp", "winter", "summer"]] # Split df_train = df[df["ds"] < "2015-03-01"] df_test = df[df["ds"] >= "2015-03-01"] ``` -------------------------------- ### Accessing Last Training Metrics Row (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/tests/debug/debug-energy-price-daily.ipynb Retrieves the last row of the `metrics` DataFrame, providing a quick view of the final training and validation performance metrics after the model fitting is complete. This is an alternative way to inspect the final metrics compared to converting to a dictionary. ```python metrics.tail(1) ``` -------------------------------- ### Retrieving Latest Forecast with Previous Steps in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/collect_predictions.ipynb This snippet extends the previous example by demonstrating how to include a specified number of preceding forecast steps along with the very latest forecast. By setting `include_previous_forecasts=5`, the output DataFrame will contain the latest forecast plus the 5 forecasts immediately prior to it. ```python df_fc = m.get_latest_forecast(forecast, include_previous_forecasts=5) df_fc.head(3) ``` -------------------------------- ### Generating Future DataFrame and Predicting with Pre-defined Quarter Conditions (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/conditional_seasonality_peyton.ipynb This snippet creates a future DataFrame for predictions, similar to the custom condition example. It then applies the `df_utils.add_quarter_condition` function to the future DataFrame to ensure the quarter-based conditions are present before using the trained NeuralProphet model to generate forecasts. ```Python future = m.make_future_dataframe(df, n_historic_predictions=365, periods=365) future = df_utils.add_quarter_condition(future) forecast = m.predict(df=future) ``` -------------------------------- ### Initializing, Fitting, and Predicting with NeuralProphet in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/_templates/index.html This Python snippet illustrates the fundamental steps for time-series forecasting using NeuralProphet. It demonstrates loading data with pandas, instantiating the NeuralProphet model, fitting it to daily frequency data, and generating a forecast. This showcases the library's ease of use for end-to-end forecasting. ```Python from neuralprophet import NeuralProphet import pandas as pd df = pd.read_csv('toiletpaper_daily_sales.csv') m = NeuralProphet() metrics = m.fit(df, freq="D") forecast = m.predict(df) ``` -------------------------------- ### Configuring Logging and Warnings for NeuralProphet Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/Migration_from_Prophet.ipynb This snippet imports necessary libraries like `logging`, `warnings`, and `pandas`, along with `NeuralProphet`. It configures logging levels to suppress verbose output from `prophet` and `neuralprophet` and ignores warnings, ensuring a cleaner console output during model training. ```Python import logging import warnings import pandas as pd # from prophet import Prophet from neuralprophet import NeuralProphet, set_log_level set_log_level("ERROR") logging.getLogger("prophet").setLevel(logging.ERROR) warnings.filterwarnings("ignore") ``` -------------------------------- ### Initializing NeuralProphet and Splitting Data (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet initializes a `NeuralProphet` model with `n_lags=24` and specific training parameters (`epochs=2`, `learning_rate=0.1`). It then splits the `df_global` DataFrame into training (`df_train`) and testing (`df_test`) sets using `m.split_df`, with a 33% validation proportion and `local_split=True` for local time series splitting. ```python m = NeuralProphet(n_lags=24, epochs=2, learning_rate=0.1) df_train, df_test = m.split_df(df_global, valid_p=0.33, local_split=True) ``` -------------------------------- ### Initializing NeuralProphet Model in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/Migration_from_Prophet.ipynb This snippet initializes a NeuralProphet model instance, specifying the quantiles for prediction intervals. The `quantiles` parameter allows defining multiple prediction intervals, here set for 90% and 10%. ```python np = NeuralProphet(quantiles=[0.90, 0.10]) ``` -------------------------------- ### Fitting NeuralProphet Model to Training Data (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet calls the `fit` method on the initialized `NeuralProphet` model (`m`), passing the `df_train` DataFrame and specifying a frequency of 'H' (hourly). It stores the training metrics in the `metrics` variable and then displays the last row of the metrics DataFrame. ```python metrics = m.fit(df_train, freq="H") metrics.tail(1) ``` -------------------------------- ### Training a NeuralProphet Model in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This snippet demonstrates how to import and initialize a NeuralProphet model, disable verbose logging, configure the plotting backend, and fit the model to a Pandas DataFrame. It specifies key parameters like `n_lags`, `ar_layers`, and `accelerator` for the model configuration, and captures training metrics. ```python # To Train # Import the NeuralProphet class from neuralprophet import NeuralProphet, set_log_level # Disable logging messages unless there is an error set_log_level("ERROR") # Create a NeuralProphet model with default parameters m = NeuralProphet(n_lags=8, ar_layers=[8, 8, 8, 8], trainer_config={"accelerator": "gpu"}) # Use static plotly in notebooks m.set_plotting_backend("plotly-resampler") # Fit the model on the dataset metrics = m.fit(df) ``` -------------------------------- ### Loading ERCOT Region Load Data (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet defines the base URL for datasets and then uses `pandas.read_csv` to load the 'load_ercot_regions.csv' file into a DataFrame named `df_ercot`. It then displays the first three rows of the loaded data to verify successful loading. ```python data_location = "https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/" df_ercot = pd.read_csv(data_location + "multivariate/load_ercot_regions.csv") df_ercot.head(3) ``` -------------------------------- ### Visualizing NeuralProphet Model Graph with PyTorchViz (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet generates a visualization of the NeuralProphet model's computation graph using `pytorchviz`. It uses `make_dot` to create a graph from the `train_epoch_prediction` method and the model's named parameters, allowing for inspection of the network architecture. The `display(fig)` call renders the generated graph. ```Python fig = make_dot(m.model.train_epoch_prediction, params=dict(m.model.named_parameters())) # fig_glob.render(filename='img/fig_glob') display(fig) ``` -------------------------------- ### Configuring Trend in Prophet Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/Migration_from_Prophet.ipynb This snippet demonstrates how to initialize a Prophet model with specific trend parameters. It sets the growth model to 'linear', defines the number of changepoints, and specifies the `changepoint_range` for trend flexibility. The model is then fitted to the loaded DataFrame `df`. ```Python # Prophet p = Prophet(growth="linear", n_changepoints=10, changepoint_range=0.5) p.fit(df) ``` -------------------------------- ### Citing NeuralProphet (BibTeX) Source: https://github.com/ourownstory/neural_prophet/blob/main/README.md Provides the BibTeX entry for citing the NeuralProphet paper 'NeuralProphet: Explainable Forecasting at Scale' in academic publications, including authors, title, year, and arXiv identifier. ```bibtex @misc{triebe2021neuralprophet, title={NeuralProphet: Explainable Forecasting at Scale}, author={Oskar Triebe and Hansika Hewamalage and Polina Pilyugina and Nikolay Laptev and Christoph Bergmeir and Ram Rajagopal}, year={2021}, eprint={2111.15397}, archivePrefix={arXiv}, primaryClass={cs.LG} } ``` -------------------------------- ### Displaying NeuralProphet Model Network Summary (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/network_architecture_visualization.ipynb This snippet uses the `display` function to show a detailed summary of the underlying PyTorch model (`m.model`) within the `NeuralProphet` object. It leverages `torchsummary.summary` to provide insights into the network's layers, output shapes, and parameter counts, aiding in debugging and understanding the model's architecture. ```python display(summary(m.model)) ``` -------------------------------- ### Configuring Trend in NeuralProphet Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/Migration_from_Prophet.ipynb This snippet shows how to initialize a NeuralProphet model with trend parameters similar to Prophet. It sets the growth to 'linear', specifies the number of changepoints, and defines the `changepoints_range`. The model is then fitted to the `df` DataFrame. ```Python # NeuralProphet np = NeuralProphet(growth="linear", n_changepoints=10, changepoints_range=0.5) np.fit(df) ``` -------------------------------- ### Initializing and Fitting a NeuralProphet Model in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/README.md This code illustrates the fundamental steps to initialize a NeuralProphet model instance, fit it to a pandas DataFrame containing historical time series data, and then generate in-sample predictions based on the fitted model. ```python m = NeuralProphet() metrics = m.fit(df) forecast = m.predict(df) ``` -------------------------------- ### Importing Libraries and Loading Data Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/mlflow.ipynb This snippet imports necessary libraries like pandas, NeuralProphet, and mlflow. It also sets the NeuralProphet log level to 'ERROR' and loads the 'air_passengers.csv' dataset from a remote URL into a pandas DataFrame. ```python import pandas as pd from neuralprophet import NeuralProphet, set_log_level, save import mlflow import time set_log_level("ERROR") data_location = "https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/" df = pd.read_csv(data_location + "air_passengers.csv") ``` -------------------------------- ### Initializing and Fitting NeuralProphet with Additive Seasonality (Python) Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/season_multiplicative_air_travel.ipynb Initializes NeuralProphet with its default additive seasonality mode, fits the model to the air passenger data, and sets the plotting backend. This demonstrates the standard behavior that proves inadequate for data with multiplicative seasonal patterns. ```python m = NeuralProphet() metrics = m.fit(df, freq="MS") m.set_plotting_backend("plotly-static") ``` -------------------------------- ### Initializing Prophet Model in Python Source: https://github.com/ourownstory/neural_prophet/blob/main/docs/source/how-to-guides/feature-guides/Migration_from_Prophet.ipynb This snippet initializes a Prophet model instance with a specified prediction interval width. The `interval_width` parameter controls the uncertainty interval for the forecast, set here to 80%. ```python p = Prophet(interval_width=0.80) ```