Path: blob/master/cpd5.2/notebooks/python_sdk/lifecycle-management/Use AutoAI and AI lifecycle to predict credit risk.ipynb
6397 views
Use AutoAI and AI lifecycle to predict credit risk with ibm-watsonx-ai
This notebook contains the steps and code to demonstrate support of AI Lifecycle features of am AutoAI model in watsonx.ai. It contains steps and code to work with ibm-watsonx-ai library available in PyPI repository. It also introduces commands for training, persisting and deploying model, scoring it, updating the model and redeploying it.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Learning goals
The learning goals of this notebook are:
Work with watsonx.ai experiments to train AutoAI models.
Persist an external model in watsonx.ai repository.
Deploy model for online scoring using client library.
Score sample records using client library.
Update previously persisted model.
Redeploy model in-place.
Contents
This notebook contains the following parts:
Install dependencies
Note: ibm-watsonx-ai
documentation can be found here.
Successfully installed wget-3.2
Requirement already satisfied: sortedcontainers~=2.2 in /opt/user-env/pyt6/lib64/python3.12/site-packages (from portion->jsonsubschema>=0.0.6->lale) (2.4.0)
Successfully installed autoai-libs-3.0.3
Requirement already satisfied: threadpoolctl>=3.1.0 in /opt/user-env/pyt6/lib64/python3.12/site-packages (from scikit-learn==1.6.1) (3.6.0)
Successfully installed ibm-watsonx-ai-1.3.20
Define credentials
Authenticate the watsonx.ai Runtime service on IBM Cloud Pak for Data. You need to provide the admin's username
and the platform url
.
Use the admin's api_key
to authenticate watsonx.ai Runtime services:
Alternatively you can use the admin's password
:
Create APIClient
instance
Working with spaces
First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data
to create one.
Click New Deployment Space
Create an empty space
Go to space
Settings
tabCopy
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, you need to set space which you will be using.
Training data connection
Define connection information to training data CSV file. This example uses the German Credit Risk dataset.
The dataset can be downloaded from here.
Download training data from git repository.
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.
Load trained model
Reading training data from COS
In this section you will learn how to deploy and score pipeline model as webservice using WML instance.
Online deployment creation
Deployment object could be printed to show basic information:
To show all available information about the deployment use the .get_params()
method:
Scoring of webservice
You can make scoring request by calling score()
on deployed pipeline.
If you want to work with the web service in an external Python application you can retrieve the service object by:
Initialize the service by
service = WebService(wml_credentials)
Get deployment_id by
service.list()
methodGet webservice object by
service.get('deployment_id')
method
After that you can call service.score()
method.
Deleting deployment
You can delete the existing deployment by calling the service.delete()
command. To list the existing web services you can use service.list()
.
5. Refine and persist new version of the model
In this section, you'll learn how to store new refinement version of your model in watsonx.ai repository.
Pipeline refinement with Lale
In this section you learn how to refine and retrain the best pipeline returned by AutoAI. There are many ways to refine a pipeline. For illustration, simply replace the final estimator in the pipeline by an interpretable model. The call to wrap_imported_operators()
augments scikit-learn operators with schemas for hyperparameter tuning.
Pipeline decomposition and new definition
Start by removing the last step of the pipeline, i.e., the final estimator.
Next, add a new final step, which consists of a choice of three estimators. In this code, |
is the or combinator (algorithmic choice). It defines a search space for another optimizer run.
New optimizer Hyperopt
configuration and training
To automatically select the algorithm and tune its hyperparameters, we create an instance of the Hyperopt
optimizer and fit
it to the data.
Save newly refined pipeline
Publish new version of the model
Download current version of the model
Create archive containing updated model
Save the current model version
Update the model
Save new model revision of the updated model
Note: Model revisions can be identified by model id
and rev
number.
Get model rev
number from creation details:
List list existing revisions of the model
Get details of updated model
6. Redeploy and score new version of the model
In this section, you'll learn how to redeploy new version of the model by using the watsonx.ai Client.
Wait for the deployment update:
Get updated deployment details
Score updated model
Use client.deployments.score()
method to run scoring.
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
You successfully completed this notebook!
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.