If you are venturing into the realm of time series analysis and need a powerful ally, look no further than GluonTS! This Python package focuses on probabilistic time series modeling using deep learning. Today, we will navigate through the installation of GluonTS and a simple example to help you understand how to unleash its potential.
Installation
To embark on your journey with GluonTS, you’ll first need to set it up on your machine. Here’s how you can install it:
- Make sure you have Python 3.7 or newer installed.
- For installation with support for PyTorch models, use:
pip install gluonts[torch]
pip install gluonts[mxnet]
For more detailed instructions, be sure to check the documentation.
Simple Example: Training a DeepAR Model
Let’s use GluonTS to train a DeepAR model using the air passengers dataset, which records monthly passenger numbers from 1949 to 1960. Our mission will be to train the model on the initial nine years and predict the remaining three years. To make it easier, let’s compare this process to preparing a gourmet meal!
The Recipe:
- First, gather all the ingredients (data).
- Next, organize your kitchen (environment) to get everything ready for cooking (modeling).
- Follow the steps carefully, knowing that the end dish will be your prediction!
Here’s the code to put this all into action:
import pandas as pd
import matplotlib.pyplot as plt
from gluonts.dataset.pandas import PandasDataset
from gluonts.dataset.split import split
from gluonts.torch import DeepAREstimator
# Load data from a CSV file into a PandasDataset
df = pd.read_csv(
"https://raw.githubusercontent.com/AileenNielsen/TimeSeriesAnalysisWithPython/master/data/AirPassengers.csv",
index_col=0,
parse_dates=True,
)
dataset = PandasDataset(df, target="#Passengers")
# Split the data for training and testing
training_data, test_gen = split(dataset, offset=-36)
test_data = test_gen.generate_instances(prediction_length=12, windows=3)
# Train the model and make predictions
model = DeepAREstimator(
prediction_length=12, freq="M", trainer_kwargs={"max_epochs": 5}
).train(training_data)
forecasts = list(model.predict(test_data.input))
# Plot predictions
plt.plot(df[1954:], color='black')
for forecast in forecasts:
forecast.plot()
plt.legend(["True values"], loc="upper left", fontsize="xx-large")
plt.show()
Just like baking a cake, where you mix ingredients in a specific order to achieve the desired outcome, this code follows a sequence to prepare your model and make predictions. Each line has its role, whether it’s importing your ingredients or plotting the cake (predictions) once it’s baked!
Troubleshooting
While the process is generally smooth, you might encounter a few bumps along the way. Here are some troubleshooting tips:
- Installation Issues: If you run into problems installing GluonTS, ensure your Python version is 3.7 or newer.
- Missing Packages: Sometimes, the required libraries may not be installed. Make sure to install PyTorch or MXNet as necessary.
- Data Errors: Ensure that your CSV file is correctly formatted and accessible; double-check the URL or the file’s path.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
GluonTS is a robust tool for time series forecasting. Whether you’re just getting started or looking to enhance your existing skills, this package can guide you through the complexities of probabilistic modeling. 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.
What’s Next?
Feel free to explore the comprehensive tutorials and documentation linked throughout this blog. Happy forecasting!
