Skip to main content

Utilities for intersectional fairness analysis of tabular health datasets.

Project description

Intersectional Fairness Toolkit for Health Machine Learning

Binder

This project provides a Python package for evaluating intersectional fairness in machine learning models applied to tabular health datasets. It supports the construction of intersectional protected groups (e.g. sex × age group) and the computation of fairness metrics such as Differential Fairness.

The toolkit is designed to be dataset-agnostic and model-agnostic, making it suitable for use in a wide range of health data science workflows.

Fairness

Fairness in machine learning refers to the ethical requirement that models do not produce systematically biased or discriminatory outcomes for individuals or groups. In the context of health data science, unfair models may lead to unequal access to diagnosis, treatment, or follow-up.

Bias can arise when model predictions differ across protected attributes such as sex, age, ethnicity, or disability status. A growing number of tools exist to help researchers and practitioners evaluate fairness in machine learning models; however, many focus on single protected attributes in isolation.

Intersectional Fairness

This package provides tools which allow researchers to evaluate fairness across intersections of protected attributes (Foulds, 2019), rather than considering each attribute independently.

For example, instead of checking:

  • men vs women
  • younger vs older patients

We check:

  • young women
  • older women
  • young men
  • older men

This approach is motivated by the observation that unfairness can be hidden when outcomes are averaged over broad groups. Disparities often emerge at the intersections of attributes, where individuals may experience compounded or 'double' disadvantage. For instance, older women may be treated less favourably than either women or older patients considered as marginal groups alone.

In this package, intersectional groups are evaluated using differential fairness metrics to quantify worst-case disparities in model outcomes.

Installation

Install from PyPI (recommended):

pip install intersectional-fairness-toolkit

Alternatively, clone the repository and install the package in editable mode:

git clone https://github.com/Raiet-Bekirov/HPDM139_assignment.git
cd HPDM139_assignment
pip install -e .

To install the package from a .ZIP file, please follow the instructions at install_and_run.md

Example usage

The example below demonstrates a typical workflow: loading a clinical dataset, training a simple classifier, and evaluating intersectional fairness metrics. The toolkit is model-agnostic and can be used with any scikit-learn–compatible estimator.

from fairness.data import load_heart_csv
from fairness.preprocess import add_age_group, preprocess_tabular, make_train_test_split
from fairness.groups import make_eval_df
from fairness.adapters import unpack_eval_df
from fairness.metrics import group_acc_ratio 
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

# 1) Load dataset and add protected attributes
df = load_heart_csv("data/heart.csv")
df = add_age_group(df)

# 2) Prepare features for modelling
df_model = preprocess_tabular(df)

# 3) Train/test split
split = make_train_test_split(df_model, target_col="HeartDisease", stratify=True)

# 4) Train a simple model (example)
model = Pipeline([
    ("scaler", StandardScaler()),
    ("clf", LogisticRegression(max_iter=2000))
])
model.fit(split.X_train, split.y_train)
y_pred = model.predict(split.X_test)

# 5) Align predictions, labels, and protected attributes
df_test = df.loc[split.X_test.index] 
eval_df = make_eval_df(
    df_test=df_test,
    protected=["Sex", "age_group"],
    y_pred=y_pred,
    y_true=split.y_test.to_numpy(),
)

# 6) Compute intersectional fairness metric

subject_labels, predictions, true_statuses = unpack_eval_df(eval_df)

acc = group_acc_ratio(
    "Sex=0|age_group=older",
    "Sex=1|age_group=older",
    subject_labels,
    predictions,
    true_statuses,
    natural_log=True
)
print("Accuracy ratio:", acc)

A complete end-to-end example using the UCI Heart Disease dataset is provided at examples/uci_heart_demo.ipynb Binder

Documentation

The following documentation is provided:

Fairness Metrics Supported

Metric function Purpose
group_acc Find the accuracy of a group with a specific label
group_acc_diff Calculate the absolute difference in accuracy between two groups
group_acc_ratio Calculate the ratio of accuracies between two groups
intersect_acc Calculate accuracy for an intersectional group
all_intersect_accs Calculate accuracies for all possible intersectional groups
max_intersect_acc_diff Calculate the maximum difference in accuracy across intersectional groups
max_intersect_acc_ratio Calculate the maximum ratio of accuracies across intersectional groups
group_fnr Find the false negative rate of a group with a specific label
group_fnr_diff Absolute difference in false negative rate between two groups
group_fnr_ratio Calculate the ratio of false negative rates between two groups
intersect_fnr Calculate false negative rate for an intersectional group
all_intersect_fnrs Calculate false negative rates for all possible intersectional groups
max_intersect_fnr_diff Calculate the maximum difference in false negative rate across intersectional groups
max_intersect_fnr_ratio Calculate the maximum ratio of false negative rates across intersectional groups
group_fpr Find the false positive rate of a group with a specific label
group_fpr_diff Absolute difference in false positive rate between two groups
group_fpr_ratio Calculate the ratio of false positive rates between two groups
intersect_fpr Calculate false positive rate for an intersectional group
all_intersect_fprs Calculate false positive rates for all possible intersectional groups
max_intersect_fpr_diff Calculate the maximum difference in false positive rate across intersectional groups
max_intersect_fpr_ratio Calculate the maximum ratio of false positive rates across intersectional groups
group_for Find the false omission rate of a group with a specific label
group_for_diff Absolute difference in false omission rate between two groups
group_for_ratio Calculate the ratio of false omission rates between two groups
intersect_for Calculate false omission rate for an intersectional group
all_intersect_fors Calculate false omission rates for all possible intersectional groups
max_intersect_for_diff Calculate the maximum difference in false omission rate across intersectional groups
max_intersect_for_ratio Calculate the maximum ratio of false omission rates across intersectional groups
group_fdr Find the false discovery rate of a group with a specific label
group_fdr_diff Absolute difference in false discovery rate between two groups
group_fdr_ratio Calculate the ratio of false discovery rates between two groups
intersect_fdr Calculate false discovery rate for an intersectional group
all_intersect_fdrs Calculate false discovery rates for all possible intersectional groups
max_intersect_fdr_diff Calculate the maximum difference in false discovery rate across intersectional groups
max_intersect_fdr_ratio Calculate the maximum ratio of false discovery rates across intersectional groups
group_to_binary Wrap single-group fairness functions so they work with intersectional groups
calculate_TP_FN_FP_TN Compute confusion-matrix counts (TP, FN, FP, TN)
calculate_TPR_TNR_FPR_FNR Compute rate metrics (TPR, TNR, FPR, FNR) from confusion-matrix counts
calculate_EOD Equal Opportunity Difference between demographic groups
calculate_AOD Average Odds Difference between demographic groups
calculate_DI Disparate Impact between demographic groups

Project context

This package was developed as part of the HPDM139 module (Health Data Science) at the University of Exeter.

Reference

License

Apache-2.0 license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

intersectional_fairness_toolkit-0.2.5.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file intersectional_fairness_toolkit-0.2.5.tar.gz.

File metadata

File hashes

Hashes for intersectional_fairness_toolkit-0.2.5.tar.gz
Algorithm Hash digest
SHA256 3c1ffda17844b430c49d16b202eb6acd280a49db67eb831a9daa041d87e8ec30
MD5 0a14546ebe087bb2d7892ded555f6b38
BLAKE2b-256 4988c82bd30e6c33cbb447f2740d5190f02ac2f570ed1ee2ea52c3dcc538dae4

See more details on using hashes here.

File details

Details for the file intersectional_fairness_toolkit-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for intersectional_fairness_toolkit-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ed75e568af2bde5227195b640defa45cbf0de66796bc2f927bc77e9063e2a524
MD5 69482431f5806bb9530634c7c1968cbf
BLAKE2b-256 aa426b925fe6599f46964a92043d15e6192359e31e1d84bd346e4a79ee120b50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page