{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# AI Automation for AI Fairness\n", "\n", "When AI models contribute to high-impact decisions such as whether or\n", "not someone gets a loan, we want them to be fair.\n", "Unfortunately, in current practice, AI models are often optimized\n", "primarily for accuracy, with little consideration for fairness. This\n", "notebook gives a hands-on example for how AI Automation can help build AI\n", "models that are both accurate and fair.\n", "This notebook is written for data scientists who have some familiarity\n", "with Python. No prior knowledge of AI Automation or AI Fairness is\n", "required, we will introduce the relevant concepts as we get to them.\n", "\n", "Bias in data leads to bias in models. AI models are increasingly\n", "consulted for consequential decisions about people, in domains\n", "including credit loans, hiring and retention, penal justice, medical,\n", "and more. Often, the model is trained from past decisions made by\n", "humans. If the decisions used for training were discriminatory, then\n", "your trained model will be too, unless you are careful. Being careful\n", "about bias is something you should do as a data scientist.\n", "Fortunately, you do not have to grapple with this issue alone. You\n", "can consult others about ethics. You can also ask yourself how your AI\n", "model may affect your (or your institution's) reputation. And\n", "ultimately, you must follow applicable laws and regulations.\n", "\n", "_AI Fairness_ can be measured via several metrics, and you need to\n", "select the appropriate metrics based on the circumstances. For\n", "illustration purposes, this notebook uses one particular fairness\n", "metric called _disparate impact_. Disparate impact is defined as the\n", "ratio of the rate of favorable outcome for the unprivileged group to\n", "that of the privileged group. To make this definition more concrete,\n", "consider the case where a favorable outcome means getting a loan, the\n", "unprivileged group is women, and the privileged group is men. Then if\n", "your AI model were to let women get a loan in 30% of the cases and men\n", "in 60% of the cases, the disparate impact would be 30% / 60% = 0.5,\n", "indicating a gender bias towards men. The ideal value for disparate\n", "impact is 1, and you could define fairness for this metric as a band\n", "around 1, e.g., from 0.8 to 1.25.\n", "\n", "To get the best performance out of your AI model, you must experiment\n", "with its configuration. This means searching a high-dimensional space\n", "where some options are categorical, some are continuous, and some are\n", "even conditional. No configuration is optimal for all domains let\n", "alone all metrics, and searching them all by hand is impossible. In\n", "fact, in a high-dimensional space, even exhaustively enumerating all\n", "the valid combinations soon becomes impractical. Fortunately, you can\n", "use tools to automate the search, thus making you more productive at\n", "finding good models quickly. These productivity and quality\n", "improvements become compounded when you have to do the search over.\n", "\n", "_AI Automation_ is a technology that assists data scientists in\n", "building AI models by automating some of the tedious steps. One AI\n", "automation technique is _algorithm selection_ , which automatically\n", "chooses among alternative algorithms for a particular task. Another AI\n", "automation technique is _hyperparameter tuning_ , which automatically\n", "configures the arguments of AI algorithms. You can use AI automation\n", "to optimize for a variety of metrics. This notebook shows you how to use AI\n", "automation to optimize for both accuracy and for fairness as measured\n", "by disparate impact.\n", "\n", "This [Jupyter](https://jupyter.org/)\n", "notebook uses the following open-source Python libraries. \n", "[AIF360](https://aif360.mybluemix.net/) \n", "is a collection of fairness metrics and bias mitigation algorithms.\n", "The [pandas](https://pandas.pydata.org/) and\n", "[scikit-learn](https://scikit-learn.org/) libraries support\n", "data analysis and machine learning with data structures and a\n", "comprehensive collection of AI algorithms.\n", "The [hyperopt](http://hyperopt.github.io/hyperopt/) library\n", "implements both algorithm selection and hyperparameter tuning for\n", "AI automation.\n", "And [Lale](https://github.com/IBM/lale) is a library for\n", "semi-automated data science; this notebook uses Lale as the backbone\n", "for putting the other libraries together.\n", "\n", "Our starting point is a dataset and a task. For illustration\n", "purposes, we picked [credit-g](https://www.openml.org/d/31), also\n", "known as the German Credit dataset. Each row describes a person\n", "using several features that may help evaluate them as a potential\n", "loan applicant. The task is to classify people into either\n", "good or bad credit risks. We load the version of the dataset from\n", "OpenML along with some fairness metadata." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Possible set intersection at position 3\n" ] } ], "source": [ "from lale.lib.aif360 import fetch_creditg_df\n", "all_X, all_y, fairness_info = fetch_creditg_df()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see what the dataset looks like, we can use off-the-shelf\n", "functionality from pandas for inspecting a few\n", "rows. The creditg dataset has a single label column, `class`, to be\n", "predicted as the outcome, which can be `good` or `bad`. Some of the\n", "feature columns are numbers, others are categoricals." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | class | \n", "checking_status | \n", "duration | \n", "credit_history | \n", "purpose | \n", "credit_amount | \n", "savings_status | \n", "employment | \n", "installment_commitment | \n", "personal_status | \n", "other_parties | \n", "residence_since | \n", "property_magnitude | \n", "age | \n", "other_payment_plans | \n", "housing | \n", "existing_credits | \n", "job | \n", "num_dependents | \n", "own_telephone | \n", "foreign_worker | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "good | \n", "<0 | \n", "6.0 | \n", "critical/other existing credit | \n", "radio/tv | \n", "1169.0 | \n", "no known savings | \n", ">=7 | \n", "4.0 | \n", "male single | \n", "none | \n", "4.0 | \n", "real estate | \n", "67.0 | \n", "none | \n", "own | \n", "2.0 | \n", "skilled | \n", "1.0 | \n", "yes | \n", "yes | \n", "
1 | \n", "bad | \n", "0<=X<200 | \n", "48.0 | \n", "existing paid | \n", "radio/tv | \n", "5951.0 | \n", "<100 | \n", "1<=X<4 | \n", "2.0 | \n", "female div/dep/mar | \n", "none | \n", "2.0 | \n", "real estate | \n", "22.0 | \n", "none | \n", "own | \n", "1.0 | \n", "skilled | \n", "1.0 | \n", "none | \n", "yes | \n", "
2 | \n", "good | \n", "no checking | \n", "12.0 | \n", "critical/other existing credit | \n", "education | \n", "2096.0 | \n", "<100 | \n", "4<=X<7 | \n", "2.0 | \n", "male single | \n", "none | \n", "3.0 | \n", "real estate | \n", "49.0 | \n", "none | \n", "own | \n", "1.0 | \n", "unskilled resident | \n", "2.0 | \n", "none | \n", "yes | \n", "
3 | \n", "good | \n", "<0 | \n", "42.0 | \n", "existing paid | \n", "furniture/equipment | \n", "7882.0 | \n", "<100 | \n", "4<=X<7 | \n", "2.0 | \n", "male single | \n", "guarantor | \n", "4.0 | \n", "life insurance | \n", "45.0 | \n", "none | \n", "for free | \n", "1.0 | \n", "skilled | \n", "2.0 | \n", "none | \n", "yes | \n", "
4 | \n", "bad | \n", "<0 | \n", "24.0 | \n", "delayed previously | \n", "new car | \n", "4870.0 | \n", "<100 | \n", "1<=X<4 | \n", "3.0 | \n", "male single | \n", "none | \n", "4.0 | \n", "no known property | \n", "53.0 | \n", "none | \n", "for free | \n", "2.0 | \n", "skilled | \n", "2.0 | \n", "none | \n", "yes | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
995 | \n", "good | \n", "no checking | \n", "12.0 | \n", "existing paid | \n", "furniture/equipment | \n", "1736.0 | \n", "<100 | \n", "4<=X<7 | \n", "3.0 | \n", "female div/dep/mar | \n", "none | \n", "4.0 | \n", "real estate | \n", "31.0 | \n", "none | \n", "own | \n", "1.0 | \n", "unskilled resident | \n", "1.0 | \n", "none | \n", "yes | \n", "
996 | \n", "good | \n", "<0 | \n", "30.0 | \n", "existing paid | \n", "used car | \n", "3857.0 | \n", "<100 | \n", "1<=X<4 | \n", "4.0 | \n", "male div/sep | \n", "none | \n", "4.0 | \n", "life insurance | \n", "40.0 | \n", "none | \n", "own | \n", "1.0 | \n", "high qualif/self emp/mgmt | \n", "1.0 | \n", "yes | \n", "yes | \n", "
997 | \n", "good | \n", "no checking | \n", "12.0 | \n", "existing paid | \n", "radio/tv | \n", "804.0 | \n", "<100 | \n", ">=7 | \n", "4.0 | \n", "male single | \n", "none | \n", "4.0 | \n", "car | \n", "38.0 | \n", "none | \n", "own | \n", "1.0 | \n", "skilled | \n", "1.0 | \n", "none | \n", "yes | \n", "
998 | \n", "bad | \n", "<0 | \n", "45.0 | \n", "existing paid | \n", "radio/tv | \n", "1845.0 | \n", "<100 | \n", "1<=X<4 | \n", "4.0 | \n", "male single | \n", "none | \n", "4.0 | \n", "no known property | \n", "23.0 | \n", "none | \n", "for free | \n", "1.0 | \n", "skilled | \n", "1.0 | \n", "yes | \n", "yes | \n", "
999 | \n", "good | \n", "0<=X<200 | \n", "45.0 | \n", "critical/other existing credit | \n", "used car | \n", "4576.0 | \n", "100<=X<500 | \n", "unemployed | \n", "3.0 | \n", "male single | \n", "none | \n", "4.0 | \n", "car | \n", "27.0 | \n", "none | \n", "own | \n", "1.0 | \n", "skilled | \n", "1.0 | \n", "none | \n", "yes | \n", "
1000 rows × 21 columns
\n", "