Path: blob/master/cloud/notebooks/python_sdk/lifecycle-management/Use AutoAI and model refinement capabilities to predict credit risk.ipynb
6405 views
Use AutoAI and model refinement capabilities 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 Runtime Service in watsonx.ai Runtime service. 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.11.
Learning goals
The learning goals of this notebook are:
Work with watsonx.ai Runtime experiments to train AutoAI models.
Persist an external model in watsonx.ai Runtime 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:
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 connection
Define connection information to COS bucket and training data CSV file. This example uses the German Credit Risk dataset.
The code in next cell uploads training data to the bucket.
Download training data from git repository.
Create connection
Note: The above connection can be initialized alternatively with api_key
and resource_instance_id
.
The above cell can be replaced with:
Define connection information to training data.
Check the connection information. Upload the data and validate.
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 watsonx.ai Runtime 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 Runtime repository by using the watsonx.ai Client.
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! You learned how to use scikit-learn machine learning as well as watsonx.ai Runtime for model creation and deployment. 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.