Welcome to the world of machine learning forecasting! If you’re looking to utilize machine learning models for time series forecasting, then you’ve landed in the right place. This guide will take you step-by-step through the setup and usage of the MLForecast framework.
Installing MLForecast
Before diving into the functionalities, you’ll need to install the MLForecast library. You can install it using either PyPI or conda-forge. Choose one of the following commands based on your preference:
- Using PyPI:
pip install mlforecast - Using conda-forge:
conda install -c conda-forge mlforecast
For more detailed instructions, you can refer to the installation page.
Quick Start
To quickly get started, use this quick guide or follow the end-to-end walkthrough for best practices.
Why Choose MLForecast?
Current Python alternatives for machine learning models can be slow and fail to scale efficiently. MLForecast addresses these issues by offering:
- Fast implementations of feature engineering for time series forecasting.
- Compatibility with popular libraries like pandas, polars, spark, dask, and ray.
- Probabilistic forecasting with conformal prediction.
- Support for various exogenous and static variables.
- An easy-to-use interface, following the familiar sklearn syntax.
How to Use the MLForecast Framework
The following outline provides a basic overview to get you up and running. For a more detailed description, please check the documentation.
Step 1: Data Setup
First, you’ll need to store your time series data in a pandas dataframe in long format. Each row must represent an observation corresponding to a specific series and timestamp.
from mlforecast.utils import generate_daily_series
series = generate_daily_series(
n_series=20,
max_length=100,
n_static_features=1,
static_as_categorical=False,
with_trend=True
)
series.head()
In this code, think of generate_daily_series as a bakery that bakes 20 different types of bread (series) each day (timestamp). Each type has a unique identifier (unique_id) with characteristics (static features) that differ from one another. The bakery keeps producing bread for a maximum of 100 days, with some of them having a trend (flavor) as they age.
Step 2: Model Definition
Next, define the models you want to utilize. They can be any regressors that fit the scikit-learn API.
import lightgbm as lgb
from sklearn.linear_model import LinearRegression
models = [
lgb.LGBMRegressor(random_state=0, verbosity=-1),
LinearRegression(),
]
Here, you’re selecting a LightGBM model and Linear Regression as your bakers who have different styles of producing bread. You’ll soon be mixing their styles to create the perfect loaf (forecast).
Step 3: Forecast Object
Now, you want to create an MLForecast object by instantiating it with your models and features.
from mlforecast import MLForecast
from mlforecast.lag_transforms import ExpandingMean, RollingMean
from mlforecast.target_transforms import Differences
fcst = MLForecast(
models=models,
freq='D',
lags=[7, 14],
lag_transforms={
1: [ExpandingMean()],
7: [RollingMean(window_size=28)]
},
date_features=['dayofweek'],
target_transforms=[Differences([1])],
)
Think of the MLForecast object as a master recipe that utilizes the selected bakers’ styles along with various ingredients (features) to create different varieties of bread each day. The lag features are like secret spices that enhance the taste of your final product.
Step 4: Training the Model
To compute features and train your models, simply call fit on your forecast object.
fcst.fit(series)
Like letting the dough rise, this step is crucial as it allows the bakers to perfect their recipe before producing bread.
Step 5: Making Predictions
To get forecasts for the next n days, call predict(n) on your forecast object.
predictions = fcst.predict(14)
And here we are, ready to savor the freshly baked bread for the next two weeks! Each loaf (forecast) will be unique, based on the learned patterns over time.
Visualizing Results
Visualizing your forecast with the series can be done using the plotting utility.
from utilsforecast.plotting import plot_series
fig = plot_series(series, predictions, max_ids=4, plot_random=False)
Visualizations help us appreciate the intricacies of our baked goods, allowing us to see how each loaf compares with the daily forecasts.
Troubleshooting Tips
- If you run into installation issues, make sure you have the latest version of pip or conda.
- In case your models face performance issues, consider reducing the data size or model complexity.
- If predictions are not as expected, check your feature engineering – it significantly affects the outcome!
- Always refer to the documentation for the most accurate information regarding functions and parameters.
For more insights, updates, or to collaborate on AI development projects, stay connected with **fxis.ai**.
At **fxis.ai**, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Conclusion
With MLForecast, you have a powerful tool for time series forecasting. Embrace the journey from data setup to model training and predictions, and experience the seamless capabilities of machine learning in forecasting. Happy forecasting!

