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 .

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.3.tar.gz (29.9 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.3.tar.gz.

File metadata

File hashes

Hashes for intersectional_fairness_toolkit-0.2.3.tar.gz
Algorithm Hash digest
SHA256 f6188fc7d0598983e4733f45a5b8511222c06c7ca273cd6a8b18fa2905b5f468
MD5 0ecd3316a56c042174f18d35f021d7ac
BLAKE2b-256 6736a629c92cfa5059150fd017cefa1c6d38ab357fa5ab3c07ee51f86fc5f313

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for intersectional_fairness_toolkit-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9afa9010c2f46ecddcc94a4367832615094878f3cd497c8ac3a7e3846e1b05c5
MD5 a53bb65d57e0291b947f9f58298587e1
BLAKE2b-256 91a1c9f7755dea2247c040bcffc7d18244a84712a43ece7314bb30d19f11039d

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