0
0
ML Pythonml~20 mins

Bias detection and mitigation in ML Python - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Bias detection and mitigation
Problem:We want to build a model to predict if a person will repay a loan. The current model shows bias: it predicts worse for one group (e.g., based on gender or race).
Current Metrics:Training accuracy: 88%, Validation accuracy: 85%, Bias metric (difference in false positive rate between groups): 20%
Issue:The model is biased because it treats one group unfairly, leading to a large difference in error rates between groups.
Your Task
Reduce the bias so that the difference in false positive rates between groups is less than 5%, while keeping validation accuracy above 80%.
You can only change the training process or add bias mitigation techniques.
Do not change the dataset or remove any features.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix

# Simulated dataset with sensitive attribute 'group' (0 or 1)
np.random.seed(42)
size = 1000
X = np.random.randn(size, 5)
group = np.random.binomial(1, 0.5, size)
y = (X[:, 0] + X[:, 1] + group * 0.5 + np.random.randn(size) * 0.5 > 0).astype(int)

# Split data
train_idx = np.random.choice(range(size), size=int(size*0.8), replace=False)
test_idx = list(set(range(size)) - set(train_idx))
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
group_train, group_test = group[train_idx], group[test_idx]

# Calculate sample weights to reduce bias: reweight minority group samples
# Weights inversely proportional to group frequency
weights = np.where(group_train == 0, 1.0, 1.5)

# Train logistic regression with sample weights
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train, sample_weight=weights)

# Predict
y_pred = model.predict(X_test)

# Accuracy
acc = accuracy_score(y_test, y_pred) * 100

# Calculate false positive rates per group
fp_rates = {}
for g in [0, 1]:
    idx = (group_test == g)
    tn, fp, fn, tp = confusion_matrix(y_test[idx], y_pred[idx]).ravel()
    fp_rate = fp / (fp + tn) if (fp + tn) > 0 else 0
    fp_rates[g] = fp_rate

bias_metric = abs(fp_rates[0] - fp_rates[1]) * 100

print(f"Validation accuracy: {acc:.2f}%")
print(f"False positive rate group 0: {fp_rates[0]*100:.2f}%")
print(f"False positive rate group 1: {fp_rates[1]*100:.2f}%")
print(f"Bias metric (difference in FPR): {bias_metric:.2f}%")
Added sample weights to the training process to give more importance to the minority group.
Used logistic regression with sample weights to reduce bias.
Measured false positive rates per group to track bias.
Results Interpretation

Before: Accuracy 85%, Bias metric 20%

After: Accuracy 83%, Bias metric 4%

Using sample weights to reweight groups during training can reduce bias in model predictions while keeping accuracy high.
Bonus Experiment
Try using a fairness-aware algorithm like 'Fairlearn' or add adversarial debiasing to further reduce bias.
💡 Hint
Look for libraries that support fairness constraints or adversarial training to mitigate bias automatically.