Path: blob/master/cloud/notebooks/python_sdk/experiments/autoai/Use AutoAI and BatchedTreeEnsemble to solve large tabular problems.ipynb
6405 views
Use AutoAI and BatchedTreeEnsemble for incremental learning of AutoAI pipelines ibm-watsonx-ai
This notebook contains the steps and code to demonstrate support of AutoAI experiments in watsonx.ai Runtime service. It introduces commands for data retrieval, training experiments, persisting pipelines, testing pipelines, refining pipelines, and scoring.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goals
The learning goals of this notebook are:
Work with watsonx.ai Runtime experiments to train AutoAI models.
Compare trained models quality and select the best one for further refinement.
Refine the best model and test new variations.
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.
2. Connections to COS
Define connection information to COS bucket and training data CSV file. This example uses the German Credit Risk dataset.
The dataset can be downloaded from here.
Action: Upload training data to COS bucket and enter location information below.
Download training data from git repository.
Create a connection
Optimizer configuration
Provide the input information for AutoAI optimizer:
name
- experiment nameprediction_type
- type of the problemprediction_column
- target column namescoring
- optimization metric
Configuration parameters can be retrieved via get_params()
.
You can use the get_run_status()
method to monitor AutoAI jobs in background mode.
You can visualize the scoring metric calculated on a holdout data set.
Get selected pipeline model
Download and reconstruct a scikit-learn pipeline model object from the AutoAI training job.
Convert the pipeline model to a Python script and download it
Visualize pipeline
Each node in the visualization is a machine-learning operator (transformer or estimator). Each edge indicates data flow (transformed output from one operator becomes input to the next). The input to the root nodes is the initial dataset and the output from the sink node is the final prediction. When you hover the mouse pointer over a node, a tooltip shows you the configuration arguments of the corresponding operator (tuned hyperparameters). When you click on the hyperlink of a node, it brings you to a documentation page for the operator.
Pipeline source code
In the pretty-printed code, >>
is the pipe combinator (dataflow edge) and &
is the and combinator (combining multiple subpipelines). They correspond to the make_pipeline
and make_union
functions from scikit-learn, respectively. If you prefer the functions, you can instead pretty-print your pipeline with best_pipeline.pretty_print(ipython_display=True, combinators=False)
.
Reading training data from COS
Test pipeline model locally
Data loader
Create DataLoader iterator to retrieve training dataset in batches (Pandas DataFrame). DataLoader is Torch
compatible (torch.utils.data
).
Note: If reading data results in an error, provide data as batches (Pandas DataFrame). It may be necessary to use methods for initial data pre-processing like: e.g. DataFrame.dropna()
, DataFrame.drop_duplicates()
, DataFrame.sample()
.
Continue model training
Fit pipeline model in batches (partial_fit
)
In this cell, the pipeline is incrementally fitted using data batches.
Note: You can also use custom code for data reading and model training. The pipeline
supports incremental learning via partial_fit
calls.
If you need, you can evaluate the pipeline using custom holdout data. Provide the X_test
, y_test
and call scorer
on them.
Test the fitted pipeline (predict
).
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
Szymon Kucharczyk, Staff Software Engineer at watsonx.ai
Mateusz Szewczyk, Software Engineer at watsonx.ai
Copyright © 2022-2025 IBM. This notebook and its source code are released under the terms of the MIT License.