Path: blob/master/cloud/notebooks/python_sdk/experiments/autoai/Use AutoAI and timeseries data with supporting features to predict PM2.5.ipynb
6405 views
Use AutoAI and timeseries data with supporting features to predict PM2.5 by ibm-watsonx-ai
This notebook contains the steps and code to demonstrate support of AutoAI experiments for timeseries data sets in watsonx.ai Runtime service. It introduces commands for data retrieval, training experiments, persisting pipelines, testing pipelines, deploying pipelines, and scoring.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goals
The learning goals of this notebook are:
Define watsonx.ai Runtime experiment for timeseries data sets with supporting features.
Work with experiments to train AutoAI models.
Compare trained models quality and select the best one for further deployment.
Online deployment and score the trained model.
Contents
This notebook contains the following parts:
1. Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Create a Cloud Object Storage (COS) instance (a lite plan is offered and information about how to order storage can be found here).
Note: When using Watson Studio, you already have a COS instance associated with the project you are running the notebook in.
Install and import the ibm-watsonx-ai
and dependecies
Note: ibm-watsonx-ai
documentation can be found here.
Connection to watsonx.ai Runtime
Authenticate the watsonx.ai Runtime service on IBM Cloud. You need to provide Cloud API key
and location
.
Tip: Your Cloud API key
can be generated by going to the Users section of the Cloud console. From that page, click your name, scroll down to the API Keys section, and click Create an IBM Cloud API key. Give your key a name and click Create, then copy the created key and paste it below. You can also get a service specific url by going to the Endpoint URLs section of the watsonx.ai Runtime docs. You can check your instance location in your watsonx.ai Runtime Service instance details.
You can use IBM Cloud CLI to retrieve the instance location
.
NOTE: You can also get a service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, and then copy the created key and paste it in the following cell.
Action: Enter your api_key
and location
in the following cell.
Working with spaces
You need to create a space that will be used for your work. If you do not have a space, you can use Deployment Spaces Dashboard to create one.
Click New Deployment Space
Create an empty space
Select Cloud Object Storage
Select watsonx.ai Runtime instance and press Create
Copy
space_id
and paste it below
Tip: You can also use SDK to prepare the space for your work. More information can be found here.
Action: assign space ID below
You can use the list
method to print all existing spaces.
To be able to interact with all resources available in watsonx.ai Runtime, you need to set the space which you will be using.
Connections to COS
In next cell we read the COS credentials from the space.
Training data sets
Download training data from git repository and upload it to a COS. This example uses the China daily PM2.5.
Visualize the data
Create connection
Note: The above connection can be initialized alternatively with api_key
and resource_instance_id
.
The above cell can be replaced with:
Training data connection
The code in next cell defines connections to created assets.
Optimizer configuration
Provide the input information for AutoAI optimizer:
name
- experiment nameprediction_type
- type of the problemprediction_columns
- target columns namestimestamp_column_name
— date&time column name/indexfeature_columns
– names/indices of supporting feature columnsforecast_window
— future date/time range to be predictedholdout_size
- number of holdout recordslookback_window
– past date/time range used for model training, -1 means auto-determinedbacktest_num
– number of backtestssupporting_features_at_forecast
– whether leveraging future values of supporting featurespipeline_types
– specify an indiviual or a group of pipelins by type
Configuration parameters can be retrieved via pipeline_optimizer.get_params()
.
You can use the get_run_status()
method to monitor AutoAI jobs in background mode.
Check pipeline details by calling get_pipeline_details()
. By default details of best pipeline are returned.
Holdout data visualization
Backtest data visualization
6. Deploy and Score
In this section you will learn how to deploy and score pipeline model as online deployment using watsonx.ai Runtime instance.
Online deployment creation
To show all available information about the deployment use the .get_params()
method:
Scoring
You can use the score
method to get predictions for defined forecasting window. You can either send payload records or use empty list.
Or you could send payload with new obeservations:
Visualization of predictions
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
8. Summary and next steps
You successfully completed this notebook!
You learned how to use ibm-watsonx-ai
to run AutoAI experiments.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors
Jun Wang, is a Software Architect and Data Scientist at IBM with a track record of developing enterprise-level applications that substantially increases clients' ability to turn data into actionable knowledge.
Mateusz Szewczyk, Software Engineer at watsonx.ai.
Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.