Path: blob/master/cloud/notebooks/python_sdk/monitoring/German credit risk prediction with Scikit for model monitoring.ipynb
6405 views
German credit risk prediciton with Scikit-learn for model monitoring
This notebook should be run in a Watson Studio project, using Default Python 3.11 runtime environment. It requires service credentials for the following Cloud services:
watsonx.ai Runtime
The notebook will train, create and deploy a German Credit Risk model.
Learning goals
In this notebook, you will learn how to:
Explore data
Prepare data for training and evaluation
Create a scikit-learn pipeline
Train and evaluate a model
Store a model in the watsonx.ai Runtime repository
Deploy and score the model
Contents
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).
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 platform api_key
and instance location
.
You can use IBM Cloud CLI to retrieve platform API Key and instance location.
API Key can be generated in the following way:
In result, get the value of api_key
from the output.
Location of your watsonx.ai Runtime instance can be retrieved in the following way:
In result, get the value of location
from the output.
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 also get service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, then copy the created key and paste it below.
Action: Enter your api_key
and location
in the following cell.
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 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 list
method to print all existing spaces.
To be able to interact with all resources available in watsonx.ai Runtime, you need to set space which you will be using.
Connections to COS
In next cell we read the COS credentials from the space.
Run the notebook
At this point, the notebook is ready to run. You can either run the cells one at a time, or click the Kernel option above and select Restart and Run All to run all the cells.
In this section you will learn how to train Scikit-learn model and next deploy it as web-service using watsonx.ai Runtime service.
Load the training data from github
As you can see, the data contains twenty one fields. Risk
field is the one you would like to predict using feedback data.
Visualize data
Save training data to Cloud Object Storage
You will start with importing required libraries
Splitting the data into train and test
Preparing the pipeline
In this step you will encode target column labels into numeric values. You can use inverse_transform
to decode numeric predictions into labels.
Train a model
Evaluate the model
In this section, the notebook uses the supplied watsonx.ai Runtime credentials to save the model (including the pipeline) to the watsonx.ai Runtime instance. Previous versions of the model are removed so that the notebook can be run again, resetting all data for another demo.
The next section of the notebook deploys the model as a RESTful web service in watsonx.ai Runtime. The deployed model will have a scoring URL you can use to send data to the model for predictions.
Score the model
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 have finished the hands-on lab for IBM watsonx.ai. You created, published and deployed Scikit-Learn german credit risk model.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
You can now run the model monitoring notebook. You need to pass deployed model id in mentioned notebook
Authors
Lukasz Cmielowski, PhD, is an Automation 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.
Szymon Kucharczyk, Software Engineer at IBM watsonx.ai.
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.