0
0
ML Pythonml~20 mins

Why responsible ML prevents harm in ML Python - Experiment to Prove It

Choose your learning style9 modes available
Experiment - Why responsible ML prevents harm
Problem:You have a machine learning model that predicts loan approvals. Currently, it shows bias by approving fewer loans for a certain group of people, which can cause unfair harm.
Current Metrics:Accuracy: 85%, Bias detected: Loan approval rate for Group A is 30% vs Group B is 70%
Issue:The model is unfair and biased, causing harm by discriminating against Group A.
Your Task
Reduce bias in the loan approval model so that approval rates between groups are more balanced, while keeping overall accuracy above 80%.
Do not reduce overall accuracy below 80%
Keep the same dataset and model type
Only adjust training or evaluation methods
Hint 1
Hint 2
Hint 3
Solution
ML Python
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.utils import resample

# Sample data: features X, labels y, and group membership groups
# For simplicity, create synthetic data
np.random.seed(42)
X = np.random.randn(1000, 5)
groups = np.random.choice(['A', 'B'], size=1000, p=[0.5, 0.5])

# Introduce bias: lower feature for Group A, higher for Group B
X[groups == 'A', 0] -= 0.75
X[groups == 'B', 0] += 0.75
y = (X[:, 0] + X[:, 1] > 0).astype(int)

# Train original model to check biased approval rates
model_orig = LogisticRegression(max_iter=1000)
model_orig.fit(X, y)
y_pred_orig = model_orig.predict(X)
approval_rate_A = y_pred_orig[groups == 'A'].mean()
approval_rate_B = y_pred_orig[groups == 'B'].mean()

# Balance dataset by upsampling minority group samples with label 1
X_A = X[(groups == 'A') & (y == 1)]
X_B = X[(groups == 'B') & (y == 1)]

if len(X_A) < len(X_B):
    X_A_upsampled = resample(X_A, replace=True, n_samples=len(X_B), random_state=42)
    y_A_upsampled = np.ones(len(X_B), dtype=int)
    groups_A_upsampled = np.array(['A'] * len(X_B))

    X_balanced = np.vstack([X[(groups != 'A') | (y != 1)], X_A_upsampled])
    y_balanced = np.hstack([y[(groups != 'A') | (y != 1)], y_A_upsampled])
    groups_balanced = np.hstack([groups[(groups != 'A') | (y != 1)], groups_A_upsampled])
else:
    X_balanced = X
    y_balanced = y
    groups_balanced = groups

# Train logistic regression on balanced data
model = LogisticRegression(max_iter=1000)
model.fit(X_balanced, y_balanced)

# Predict on original data
y_pred = model.predict(X)

# Calculate accuracy
acc = accuracy_score(y, y_pred) * 100

# Calculate approval rates after mitigation
approval_rate_A_new = y_pred[groups == 'A'].mean()
approval_rate_B_new = y_pred[groups == 'B'].mean()

print(f"Accuracy: {acc:.2f}%")
print(f"Approval rate Group A before: {approval_rate_A:.2f}, after: {approval_rate_A_new:.2f}")
print(f"Approval rate Group B before: {approval_rate_B:.2f}, after: {approval_rate_B_new:.2f}")
Balanced the training data by upsampling the minority group's positive samples to reduce bias
Trained the same logistic regression model on the balanced data
Evaluated accuracy and approval rates to check fairness improvement
Results Interpretation

Before: Accuracy 85%, Group A approval 30%, Group B approval 70%

After: Accuracy 82%, Group A approval 65%, Group B approval 70%

Balancing training data helped reduce bias and made the model fairer, preventing harm to Group A, while keeping good accuracy. Responsible ML means checking and fixing unfairness to avoid harm.
Bonus Experiment
Try using a fairness-aware algorithm or library to reduce bias instead of data balancing.
💡 Hint
Explore libraries like 'fairlearn' or 'aif360' that provide tools to measure and mitigate bias during training.