Bias Detection in ML Models
Protected attributes, disparate impact, fairlearn metrics, equalized odds, demographic parity.
Bias Detection in ML Models is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Algorithmic Bias
A model is biased when its predictions systematically disadvantage a group defined by a sensitive attribute (gender, race, age). Bias often comes from skewed training data and can cause real harm in hiring, lending, or healthcare decisions.
Sensitive Features
A sensitive feature is the attribute across which we measure fairness, such as gender or ethnicity. Fairness metrics compare model behavior across the groups this feature defines, even if the feature is not used as a model input.
Demographic Parity
Demographic parity requires the positive prediction rate to be equal across groups. If a loan model approves 60% of group A but only 35% of group B, demographic parity is violated regardless of the applicants true creditworthiness.
# P(prediction = 1 | group = A) == P(prediction = 1 | group = B)Equalized Odds
Equalized odds is stricter: it requires equal true-positive and false-positive rates across groups. This conditions on the actual label, so it permits different selection rates as long as the model is equally accurate for each group.
# TPR and FPR equal across groups
# P(pred=1 | y=1, A) == P(pred=1 | y=1, B)
# P(pred=1 | y=0, A) == P(pred=1 | y=0, B)Parity vs Odds
These metrics can conflict:
- Demographic parity ignores the true label, so it may force equal rates even when base rates genuinely differ
- Equalized odds respects the true label but allows unequal selection rates
No single metric is universally correct; the choice depends on context and law.
Introducing fairlearn
fairlearn is a Python library for measuring and mitigating unfairness. Its core measurement tool is the MetricFrame, which computes any metric broken down by sensitive group.
import pandas as pd
from fairlearn.metrics import MetricFrame
from sklearn.metrics import accuracy_score, selection_rateBuilding a MetricFrame
You pass a dictionary of metrics, the true labels, the predictions, and the sensitive_features. fairlearn evaluates every metric per group.
mf = MetricFrame(
metrics={
"accuracy": accuracy_score,
"selection_rate": selection_rate,
},
y_true=y_test,
y_pred=y_pred,
sensitive_features=X_test["gender"],
)Per-Group Results
The by_group attribute returns a table of each metric for each group, making disparities obvious at a glance.
print(mf.by_group)
# accuracy selection_rate
# gender
# female 0.81 0.34
# male 0.83 0.59Overall and Differences
fairlearn also reports aggregate views: overall for the whole dataset, and helpers like difference() and ratio() that summarize the gap between the best and worst groups.
print(mf.overall)
print(mf.difference(method="between_groups"))
# selection_rate 0.25 -> 25 point gap between groupsVisualizing Disparities
A bar chart of by_group turns numbers into an intuitive picture. A tall gap between bars for the same metric is a clear red flag that the model treats groups differently.
mf.by_group.plot.bar(
subplots=True,
layout=[1, 2],
figsize=(10, 4),
title="Metrics by group",
)From Detection to Mitigation
Once you have quantified bias, fairlearn offers mitigation algorithms (such as ExponentiatedGradient and post-processing with ThresholdOptimizer) that retrain or adjust thresholds to satisfy a fairness constraint. Detection always comes first: you cannot fix what you have not measured.
Quick Check
Test your fairness knowledge.
Recap
You learned to detect bias:
- Demographic parity equalizes positive rates across groups
- Equalized odds equalizes TPR and FPR, conditioning on the label
- fairlearn MetricFrame computes a metrics dict per
sensitive_featuresgroup - Visualize
by_groupto spot disparities, then apply mitigation
Frequently asked questions
Is the “Bias Detection in ML Models” lesson free?
Yes — the full text of “Bias Detection in ML Models” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Bias Detection in ML Models”?
Protected attributes, disparate impact, fairlearn metrics, equalized odds, demographic parity. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Bias Detection in ML Models” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.