Phase 2: Bias Detection & Fairness Metrics · 50 min · Python · Fairlearn · AIF360
Bias Mitigation — Pre-processing, In-processing, Post-processing
Detecting bias is diagnosis. Mitigating it is treatment. You need both.
Hiring signal: Being able to apply bias mitigation techniques and measure the accuracy-fairness tradeoff is the skill that separates RAI auditors from RAI engineers. Interviewers ask 'you found bias, now what?' — and the answer needs to include specific mitigation algorithms and their measured tradeoffs.
What you will learn
- Apply reweighing (pre-processing) to balance the dataset
- Apply exponentiated gradient reduction (in-processing) during model training
- Apply threshold optimization (post-processing) to adjust decision boundaries
- Measure and plot the accuracy-fairness tradeoff across all three techniques
The Problem
You've run the bias audit. The numbers show disparate impact below 0.80 and equalized odds difference above 0.05. The model is biased. Now what?
Bias mitigation is where RAI engineering gets real. You have three intervention points — before training (pre-processing), during training (in-processing), and after training (post-processing). Each has tradeoffs:
| Intervention | When | What It Changes | Pros | Cons |
|---|
| Pre-processing | Before training | The training data | Simple, model-agnostic, preserves the original model | Doesn't fix model-internal bias; limited effectiveness |
| In-processing | During training | The training objective | Most effective; directly optimizes for fairness | Model-specific; harder to implement; may significantly reduce accuracy |
| Post-processing | After training | The decision threshold | Simple, model-agnostic, no retraining needed | Doesn't fix the model; may be legally problematic (explicitly using protected attribute at decision time) |
The 3 Techniques You'll Build
1. Reweighing (Pre-processing)
Reweighing assigns weights to training examples so that the joint distribution of the protected attribute and the label matches what it would be if they were independent. In simpler terms: it upweights under-represented combinations (e.g., Group_B with positive outcome) and downweights over-represented ones.
from fairlearn.preprocessing import CorrelationRemover
# Or use AIF360's Reweighing algorithm:
from aif360.algorithms.preprocessing import Reweighing
2. Exponentiated Gradient Reduction (In-processing)
This technique wraps your model in a fairness-constrained optimization. It trains the model while penalizing fairness violations, finding the best accuracy-fairness tradeoff on the Pareto frontier.
from fairlearn.reductions import ExponentiatedGradient, DemographicParity, EqualizedOdds
mitigator = ExponentiatedGradient(
estimator=RandomForestClassifier(),
constraints=EqualizedOdds(), # or DemographicParity()
)
mitigator.fit(X_train, y_train, sensitive_features=sensitive_train)
y_pred_mitigated = mitigator.predict(X_test)
3. Threshold Optimization (Post-processing)
This technique finds different decision thresholds for each group to equalize a fairness metric. It's the simplest to implement but the most legally sensitive — it explicitly uses the protected attribute at decision time.
from fairlearn.postprocessing import ThresholdOptimizer
postprocess = ThresholdOptimizer(
estimator=model, # pre-trained model
constraints="equalized_odds",
)
postprocess.fit(X_train, y_train, sensitive_features=sensitive_train)
y_pred_post = postprocess.predict(X_test, sensitive_features=sensitive_test)
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Building the Mitigation Comparison Module, What's Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy