Path: blob/master/cpd4.0/notebooks/python_sdk/experiments/autoai/fairness/Bias detection and mitigation in AutoAI.ipynb
6397 views
Bias detection and mitigation in AutoAI
This notebook contains the steps and code to demonstrate support of AutoAI experiments with bias detection/mitigation in Watson Machine Learning service. It introduces commands for data retrieval, training experiments, persisting pipelines, testing pipelines and scoring.
Some familiarity with Python is helpful. This notebook uses Python 3.9.
Learning goals
The learning goals of this notebook are:
Work with Watson Machine Learning experiments to train AutoAI models.
Compare trained models quality and select the best one for further refinement.
Investigate models fairness.
Online deployment and score the trained model.
Contents
This notebook contains the following parts:
Connection to WML
Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url
, your username
and api_key
.
Alternatively you can use username
and password
to authenticate WML services.
Install and import the ibm-watson-machine-learning
package
Note: ibm-watson-machine-learning
documentation can be found here.
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 Watson Machine Learning, you need to set the space which you will be using.
Training data connection
This example uses the German Credit Risk dataset.
The dataset can be downloaded from here.
Download training data from git repository and create data assets.
Bias detection: optimizer configuration
Provide input information for AutoAI optimizer:
name
- experiment nameprediction_type
- type of the problemprediction_column
- target column namescoring
- optimization metric
Define fairness_info
:
protected_attribute_names (array of items : string) – Subset of feature names for which fairness is desired.
favorable_labels (array of union) – Label values which are considered favorable (i.e. “positive”). Available types string Literal value number Numerical value array of number, >= 2 items, <= 2 items Numeric range [a,b] from a to b inclusive.
Configuration parameters can be retrieved via get_params()
.
You can use the get_run_status()
method to monitor AutoAI jobs in background mode.
Get experiment training_id
.
Get selected pipeline model
Download and reconstruct a scikit-learn pipeline model object from the AutoAI training job.
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.
Reading training data
Test pipeline model locally
Define fairness_info
for disparate_impact
scorer.
Bias mitigation: optimizer configuration
Provide input information for AutoAI optimizer:
name
- experiment nameprediction_type
- type of the problemprediction_column
- target column namescoring
- optimization metric
Define fairness_info
:
protected_attribute_names (array of items : string) – Subset of feature names for which fairness is desired.
favorable_labels (array of union) – Label values which are considered favorable (i.e. “positive”). Available types string Literal value number Numerical value array of number, >= 2 items, <= 2 items Numeric range [a,b] from a to b inclusive.
Configuration parameters can be retrieved via get_params()
.
You can use the get_run_status()
method to monitor AutoAI jobs in background mode.
Get selected pipeline model
Download and reconstruct a scikit-learn pipeline model object from the AutoAI training job.
Visualize pipeline
Reading training data
Test pipeline model locally
Define fairness_info
for disparate_impact
and accuracy_and_disparate_impact
scorers.
Deployment creation
Deployment object could be printed to show basic information:
To show all available information about deployment use .get_params()
method:
Scoring of webservice
You can make scoring request by calling score()
on deployed pipeline.
If you want to work with the webservice in external Python application you can retrieve the service object by:
initialize 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 exeiting deployment by calling service.delete()
command. To list existing web services you can use service.list()
.
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
Summary and next steps
You successfully completed this notebook!.
You learned how to use watson-machine-learning-client
to run AutoAI experiments.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors
Dorota Dydo-Rożniecka, Intern in Watson Machine Learning at IBM.
Szymon Kucharczyk, Software Engineer in Watson Machine Learning at IBM.
Copyright © 2021-2025 IBM. This notebook and its source code are released under the terms of the MIT License.