Path: blob/master/cpd4.6/notebooks/python_sdk/experiments/autoai/fairness/Use AutoAI to train fair models.ipynb
6408 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 experiment to train AutoAI models with bias detection and mitigation.
Compare trained models quality and fairness.
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
and dependencies
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 list
method to print all existing spaces.
Training data sets
Define connection information to training data CSV file.
Download training data from git repository.
Create data asset
Bias detection and mitigation
Terms and definitions:
Fairness Attribute
- Bias or fairness is typically measured using some fairness attribute such as Gender, Ethnicity, Age, etc.
Monitored/Reference Group
- Monitored group are those values of fairness attribute for which we want to measure bias. The rest of the values of the fairness attributes are called as reference group. In case of Fairness Attribute=Gender, if we are trying to measure bias against females, then Monitored group is “Female” and Reference group is “Male”.
Favourable/Unfavourable outcome
- An important concept in bias detection is that of favourable and unfavourable outcome of the model. E.g., Claim approved can be considered as a favourable outcome and Claim denied can be considered as an unfavourable outcome.
Disparate Impact
- metric used to measure bias (computed as the ratio of percentage of favourable outcome for the monitored group to the percentage of favourable outcome for the reference group). Bias is said to exist if the disparate impact value is below some threshold.
Optimizer configuration
Provide input information for AutoAI optimizer:
name
- experiment nameprediction_type
- type of the problemprediction_column
- target column namefairness_info
- bias detection configurationscoring
-accuracy_and_disparate_impact
combined optimization metric for both accuracy and fairness. For regression learning problem ther2_and_disparate_impact
metric is supported (combines r2 and fairness).
fairness_info
definition:
protected_attributes
(list of dicts) – subset of features for which fairness calculation is desired.feature
- name of feature for whichreference_group
andmonitored_group
are specified.reference_group
andmonitored_group
- monitored group are those values of fairness attribute for which we want to measure bias. The rest of the values of the fairness attribute are reference group.
favorable_labels
andunfavorable_labels
– label values which are considered favorable (i.e. “positive”).unfavorable_labels
are required when prediction type is regression.
Examples of supported configuration:
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
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.
Read the data
Calculate metrics
For detail description of used metrics you can check the documentation:
Fairness insights
You can analize favorable outcome distributions using visualize
method from utils
module.
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!
As a next step you can deploy and score the model: Sample notebook.
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.
Dorota Lączak, software engineer 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.