Fairlearn

On this page 7

Fairlearn is an open-source Python toolkit for assessing and improving the fairness of machine learning models, with a focus on group fairness: whether a model performs or allocates outcomes differently across groups defined by sensitive features such as sex, age band or ethnicity. Among AI frameworks & tools in artificial intelligence, it occupies the narrow but important niche of fairness evaluation and mitigation for classical supervised models.

The project began at Microsoft Research and is now developed as a community-governed project under the permissive MIT license. It follows scikit-learn conventions, so its estimators wrap existing models with familiar fit and predict methods, and its metrics accept the same arrays that ordinary evaluation code already uses.

How Fairlearn structures a fairness assessment

Fairlearn assesses fairness by disaggregating standard metrics by group and then summarizing the differences. The central object is MetricFrame, which takes true labels, predictions, a dictionary of metric functions and one or more sensitive feature columns, and returns per-group values, overall values and gaps between groups.

From a MetricFrame a practitioner can read the largest difference or the smallest ratio between groups for any metric, such as recall, false positive rate or selection rate. Ready-made summary functions such as demographic_parity_difference and equalized_odds_difference compute the most common disparity measures directly. Results can be plotted per group, which makes disparities visible to reviewers who do not read code.

The following table lists the main building blocks of the library and the role each one plays.

ComponentModulePurpose
MetricFramefairlearn.metricsDisaggregates any metric by sensitive group
Disparity metricsfairlearn.metricsSummarizes gaps as a difference or ratio
Reduction algorithmsfairlearn.reductionsRetrains a model under a fairness constraint
ThresholdOptimizerfairlearn.postprocessingSets group-specific decision thresholds
CorrelationRemoverfairlearn.preprocessingRemoves linear correlation with sensitive features
Benchmark datasetsfairlearn.datasetsLoads public data for experiments and teaching
Core components of the Fairlearn library

Fairness definitions supported by Fairlearn

Fairlearn expresses fairness as parity constraints on group-level statistics, and the choice of constraint encodes a value judgment about which harm matters. Demographic parity asks that the selection rate be similar across groups, while equalized odds asks that both true positive and false positive rates be similar. These criteria generally cannot all hold at once when base rates differ between groups, a result known from the fairness literature as an impossibility theorem.

The table summarizes the constraints that the reduction algorithms accept and the type of harm each one targets.

ConstraintEqualized quantityTypical harm addressed
Demographic paritySelection rateAllocation of opportunities
Equalized oddsTrue and false positive ratesUnequal error burden
True positive rate parityRecall for the positive classMissed qualified candidates
False positive rate parityRate of wrong positive decisionsUnwarranted flags or denials
Error rate parityOverall misclassification rateUneven quality of service
Bounded group lossWorst-group loss in regressionPoor accuracy for one group
Parity constraints and the harms they address

Fairlearn distinguishes allocation harms, where a system withholds resources or opportunities, from quality-of-service harms, where it simply works worse for some people. The documentation stresses that fairness is a sociotechnical property, so a metric gap is a signal for investigation, not a verdict on its own.

Mitigation algorithms and when each applies

Fairlearn offers mitigation at three points of the pipeline: before training, during training and after training. The reductions approach, based on research by Agarwal and colleagues, treats fair classification as a sequence of cost-sensitive learning problems and repeatedly retrains the base estimator with reweighted samples.

ExponentiatedGradient returns a randomized mixture of models that satisfies the constraint up to a tolerance, while GridSearch returns a set of deterministic models along a fairness-accuracy trade-off curve. ThresholdOptimizer, based on the work of Hardt and colleagues, leaves the model untouched and chooses group-specific thresholds, which requires the sensitive feature at prediction time.

TechniquePipeline stageBest suited whenMain drawback
CorrelationRemoverPreprocessingFeatures leak group membership linearlyIgnores nonlinear proxies
ExponentiatedGradientIn-processingRetraining is possible and cheapRandomized predictions
GridSearchIn-processingSeveral candidate models are neededHigh training cost
ThresholdOptimizerPostprocessingThe model cannot be retrainedNeeds group data at inference
Choosing a mitigation technique

Reduction methods work with any estimator that accepts sample weights, including logistic regression, gradient boosting from LightGBM or XGBoost and many scikit-learn models. Deep learning models trained in PyTorch or TensorFlow can be wrapped, but retraining cost grows quickly because each iteration fits a new model.

Fairlearn is one of several fairness toolkits, and its distinguishing traits are a compact API, tight scikit-learn integration and an emphasis on assessment over automation. The closest neighbor is AI Fairness 360, which covers a wider catalog of metrics and algorithms.

ToolScopeIntegration styleTypical user
FairlearnGroup fairness metrics and mitigationscikit-learn estimatorsData scientists in Python
AI Fairness 360Broad catalog of bias metrics and algorithmsOwn dataset classesResearchers comparing methods
Responsible AI ToolboxDashboards for error and fairness analysisInteractive widgetsModel reviewers
scikit-learn aloneStandard metrics without groupingNativeTeams with custom audit code
Fairlearn and neighboring tools

Earlier releases shipped an interactive fairness dashboard, which was later moved out of the core package into the separate Responsible AI Toolbox. Current Fairlearn keeps plotting through MetricFrame and Matplotlib instead, which keeps the library light and easy to run in notebooks such as Google Colab.

Limitations and common mistakes

Fairlearn measures statistical parity between predefined groups, and its results are only as meaningful as the groups, labels and metrics chosen. Practitioners most often run into the following problems.

  • Missing sensitive data: many organizations do not collect the attributes needed for assessment, and inferring them introduces new errors.
  • Small group sizes: disparity estimates for groups with a few dozen samples have wide confidence intervals, so bootstrapping or caution is required.
  • Biased labels: if historical outcomes encode discrimination, achieving parity against those labels can preserve the original bias.
  • Intersectional gaps: auditing sex and age separately can hide harm to a specific combination, so passing several columns to MetricFrame is advisable.
  • Legal constraints: using a sensitive feature at decision time, as ThresholdOptimizer does, may be restricted by anti-discrimination law in some jurisdictions.

Mitigation also has a cost. Reducing a disparity usually lowers overall accuracy somewhat, and the size of that cost depends on the data. Teams should document the chosen constraint, the tolerance and the reasons in model cards or audit records rather than presenting a mitigated model as fair in an absolute sense.

Stripped to the essentials

Fairlearn is a free toolkit for checking whether a model treats groups differently. Instead of reporting one accuracy number, it splits the results by group, for example women and men or younger and older applicants, and shows where the model makes more mistakes or says yes less often.

If it finds a gap, it offers several ways to shrink it, such as retraining the model with extra rules or adjusting the cutoff score for each group. It cannot decide what fair means for a given situation. People still have to choose the right measure and judge whether the result is acceptable.

Where it is applied

A regional lender uses a gradient-boosted credit model to approve small personal loans. Before deployment, its data science team loads about 40,000 historical applications into a MetricFrame with age band and sex as sensitive features. The audit shows a selection rate of 52 percent for applicants over 60 against 64 percent for applicants aged 30 to 45, and a noticeably higher false negative rate for the older group.

The team retrains the model with ExponentiatedGradient under a true positive rate parity constraint, so that creditworthy applicants in every age band are approved at similar rates. The gap in true positive rates falls from about 11 points to under 3, while overall accuracy drops by less than one point. The audit record and chosen constraint are filed with the risk committee, which approves the model with a quarterly fairness review.