Path: blob/master/cloud/notebooks/python_sdk/monitoring/Monitor credit risk model with Watson Openscale.ipynb
6405 views
Monitor german credit risk model with Watson OpenScale
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
Watson OpenScale
If you have a paid Cloud account, you may also provision a Databases for PostgreSQL or Db2 Warehouse service to take full advantage of integration with Watson Studio and continuous learning services. If you choose not to provision this paid service, you can use the free internal PostgreSQL storage with OpenScale, but will not be able to configure continuous learning for your model.
The notebook will configure OpenScale to monitor that deployment, and inject seven days' worth of historical records and measurements for viewing in the OpenScale Insights dashboard.
Prerequisite
In order to execute this notebook you will need a deployed model. You can perform it with following notebook. Then you will need to copy deployment_id
to this notebook.
Learning goals
In this notebook, you will learn how to:
Retrieve existing deployment in Watson OpenScale
Make a WatsonOpenscale subscription
Prepare and run model monitoring and feedback logging
Run fairness monitor
Run drift monitor
Run custom monitors and metrics
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.
List deployments.
Put deployment_id
of model deployed in prerequisite notebook in the next cell.
Get deployment_url
and model_id
from deployment details.
Install and import the ibm-watson-openscale
package
Note: ibm-watson-openscale
documentation can be found here.
This tutorial can use Databases for PostgreSQL, Db2 Warehouse, or a free internal verison of PostgreSQL to create a datamart for OpenScale.
If you have previously configured OpenScale, it will use your existing datamart, and not interfere with any models you are currently monitoring. Do not update the cell below.
If you do not have a paid Cloud account or would prefer not to provision this paid service, you may use the free internal PostgreSQL service with OpenScale. Do not update the cell below.
To provision a new instance of Db2 Warehouse, locate Db2 Warehouse in the Cloud catalog, give your service a name, and click Create. Once your instance is created, click the Service Credentials link on the left side of the screen. Click the New credential button, give your credentials a name, and click Add. Your new credentials can be accessed by clicking the View credentials button. Copy and paste your Db2 Warehouse credentials into the cell below.
To provision a new instance of Databases for PostgreSQL, locate Databases for PostgreSQL in the Cloud catalog, give your service a name, and click Create. Once your instance is created, click the Service Credentials link on the left side of the screen. Click the New credential button, give your credentials a name, and click Add. Your new credentials can be accessed by clicking the View credentials button. Copy and paste your Databases for PostgreSQL credentials into the cell below.
If you previously configured OpenScale to use the free internal version of PostgreSQL, you can switch to a new datamart using a paid database service. If you would like to delete the internal PostgreSQL configuration and create a new one using service credentials supplied in the cell above, set the KEEP_MY_INTERNAL_POSTGRES variable below to False below. In this case, the notebook will remove your existing internal PostgreSQL datamart and create a new one with the supplied credentials. NO DATA MIGRATION WILL OCCUR.
In next cells, you will need to paste some credentials to Cloud Object Storage. If you haven't worked with COS yet please visit getting started with COS tutorial. You can find COS_API_KEY_ID
and COS_RESOURCE_CRN
variables in Service Credentials in menu of your COS instance. Used COS Service Credentials must be created with Role parameter set as Writer. Later training data file will be loaded to the bucket of your instance and used as training refecence in subsription.
COS_ENDPOINT
variable can be found in Endpoint field of the menu.
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.
Load the training data from github
Retrieve data filename, model name and deployment name.
The notebook will now import the necessary libraries and set up a Python OpenScale client.
Create schema and datamart
Set up datamart
Watson OpenScale uses a database to store payload logs and calculated metrics. If database credentials were not supplied above, the notebook will use the free, internal lite database. If database credentials were supplied, the datamart will be created there unless there is an existing datamart and the KEEP_MY_INTERNAL_POSTGRES variable is set to True. If an OpenScale datamart exists in Db2 or PostgreSQL, the existing datamart will be used and no data will be overwritten.
Prior instances of the German Credit model will be removed from OpenScale monitoring.
Remove existing service provider connected with used watsonx.ai Runtime instance.
Multiple service providers for the same engine instance are avaiable in Watson OpenScale. To avoid multiple service providers of used watsonx.ai Runtime instance in the tutorial notebook the following code deletes existing service provder(s) and then adds new one.
Add service provider
Watson OpenScale needs to be bound to the watsonx.ai Runtime instance to capture payload data into and out of the model.
Note: You can bind more than one engine instance if needed by calling wos_client.service_providers.add
method. Next, you can refer to particular service provider using service_provider_id
.
Subscriptions
Remove existing credit risk subscriptions
This code removes previous subscriptions to the German Credit model to refresh the monitors with the new model and new data.
Following cells create the model subscription in OpenScale using the Python client API. Note that we need to provide the model unique identifier, and some information about the model itself.
Get subscription list
Score the model so we can configure monitors
Now that the watsonx.ai Runtime service has been bound and the subscription has been created, we need to send a request to the model before we configure OpenScale. This allows OpenScale to create a payload log in the datamart with the correct schema, so it can capture data coming into and out of the model. The sends a few records for predictions.
Enable quality monitoring
The code below waits ten seconds to allow the payload logging table to be set up before it begins enabling monitors. First, it turns on the quality (accuracy) monitor and sets an alert threshold of 70%. OpenScale will show an alert on the dashboard if the model accuracy measurement (area under the curve, in the case of a binary classifier) falls below this threshold.
The second paramater supplied, min_records, specifies the minimum number of feedback records OpenScale needs before it calculates a new measurement. The quality monitor runs hourly, but the accuracy reading in the dashboard will not change until an additional 50 feedback records have been added, via the user interface, the Python client, or the supplied feedback endpoint.
Feedback logging
The code below downloads and stores enough feedback data to meet the minimum threshold so that OpenScale can calculate a new accuracy measurement. It then kicks off the accuracy monitor. The monitors run hourly, or can be initiated via the Python API, the REST API, or the graphical user interface.
Get feedback logging dataset ID
Fairness configuration
The code below configures fairness monitoring for our model. It turns on monitoring for two features, Sex and Age. In each case, we must specify:
Which model feature to monitor
One or more majority groups, which are values of that feature that we expect to receive a higher percentage of favorable outcomes
One or more minority groups, which are values of that feature that we expect to receive a higher percentage of unfavorable outcomes
The threshold at which we would like OpenScale to display an alert if the fairness measurement falls below (in this case, 95%)
Additionally, we must specify which outcomes from the model are favourable outcomes, and which are unfavourable. We must also provide the number of records OpenScale will use to calculate the fairness score. In this case, OpenScale's fairness monitor will run hourly, but will not calculate a new fairness rating until at least 200 records have been added. Finally, to calculate fairness, OpenScale must perform some calculations on the training data, so we provide the dataframe containing the data.
Drift configuration
Score the model again now that monitoring is configured
This next section randomly selects 200 records from the data feed and sends those records to the model for predictions. This is enough to exceed the minimum threshold for records set in the previous section, which allows OpenScale to begin calculating fairness.
Score 200 randomly chosen records
Run fairness monitor
Kick off a fairness monitor run on current data. The monitor runs hourly, but can be manually initiated using the Python client, the REST API, or the graphical user interface.
Run drift monitor
Kick off a drift monitor run on current data. The monitor runs every hour, but can be manually initiated using the Python client, the REST API.
Configure Explainability
Finally, we provide OpenScale with the training data to enable and configure the explainability features.
Run explanation for sample record
Register custom monitor
Show available monitors types
Get monitors ids and details
Enable custom monitor for subscription
Get monitor instance id and configuration details
Storing custom metrics
List and get custom metrics
Identify transactions for Explainability
Transaction IDs identified by the cells below can be copied and pasted into the Explainability tab of the OpenScale dashboard.
You successfully completed this notebook!
You have finished the hands-on lab for IBM Watson OpenScale. You can now view the OpenScale Dashboard. Click on the tile for the German Credit model to see fairness, accuracy, and performance monitors. Click on the timeseries graph to get detailed information on transactions during a specific time window.
OpenScale shows model performance over time. You have two options to keep data flowing to your OpenScale graphs:
Download, configure and schedule the model feed notebook. This notebook can be set up with your watsonx.ai credentials, and scheduled to provide a consistent flow of scoring requests to your model, which will appear in your OpenScale monitors.
Re-run this notebook. Running this notebook from the beginning will delete and re-create the model and deployment, and re-create the historical data. Please note that the payload and measurement logs for the previous deployment will continue to be stored in your datamart, and can be deleted if necessary. You can use this notebooks cells to delete deployments.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
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.
Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.