AI, ML & Data Science

Part 5 of the Explainable AI Blog Series: Building Fair and Transparent AI: Detecting and Mitigating Bias with XAI Tools

This entry is part 5 of 5 in the series Explainable AI

📝 This Blog is Part 5 of the Explainable AI Blog Series

In the previous blogs, we explored the fundamentals of Explainable AI (XAI) tools like LIME and SHAP, delving into their role in interpreting predictions. This blog will take it a step further by tackling bias detection and mitigation in AI models—a critical aspect of ethical AI.

By the end of this blog, you’ll:

  1. Understand how biases manifest in AI models.
  2. Use LIME and SHAP to detect potential biases in a loan approval model.
  3. Implement techniques to mitigate biases and evaluate their impact.

Why Bias Detection Matters in AI

AI systems are only as unbiased as the data and processes that train them. Bias can creep in through:

  • Historical Prejudice: Training data reflecting historical inequalities (e.g., lower loan approvals for specific demographics).
  • Sampling Bias: Imbalanced representation of groups in the dataset.
  • Feature Selection: Correlations between irrelevant features and the target variable.

📌 Real-Life Consequences of Bias

  1. Unfair Loan Decisions: Applicants from underrepresented groups might face unjust denials.
  2. Regulatory Scrutiny: Models that discriminate can violate laws like GDPR and Equal Credit Opportunity Act.
  3. Loss of Trust: Perceived unfairness can damage customer relationships and reputation.

Step 1: Revisit the Loan Approval Model

We’ll build on the loan approval model from earlier blogs. Ensure you have the model and dataset ready. If not, refer to Part 2 for setup instructions.

python
import joblib
import pandas as pd

# Load the trained model and test data
model = joblib.load('loan_model.pkl')
X_test = pd.read_csv('X_test.csv')


Step 2: Detect Bias with SHAP

2.1 Generate Global SHAP Values

Start by identifying which features contribute most to the model’s decisions:

python
import shap
explainer = shap.Explainer(model.predict, X_test)
shap_values = explainer(X_test)

# Visualize global feature importance
shap.summary_plot(shap_values, X_test)

📊 Insights from the Summary Plot:

For a biased model, you might notice:

  • Overreliance on Credit_History, disadvantaging applicants without established credit.
  • High LoanAmount values penalizing low-income groups disproportionately.

2.2 Detect Local Bias

Analyze individual predictions for fairness. For example, compare SHAP force plots of two applicants with similar profiles but different demographics.

python
shap.force_plot(
explainer.expected_value[1],
shap_values[1][0],
X_test.iloc[0],
matplotlib=True
)

Observation: If two applicants with identical incomes and loan amounts have different outcomes due to Credit_History, this indicates bias.


Step 3: Detect Bias with LIME

3.1 Explain Individual Predictions

Use LIME to compare the explanations for similar applicants.

python
from lime.lime_tabular import LimeTabularExplainer

explainer = LimeTabularExplainer(
training_data=X_test.values,
feature_names=['ApplicantIncome', 'LoanAmount', 'Credit_History'],
class_names=['Denied', 'Approved'],
mode='classification'
)

# Generate explanations for specific predictions
explanation = explainer.explain_instance(
X_test.iloc[0].values,
model.predict_proba
)
explanation.show_in_notebook()

Observation: LIME highlights if certain features disproportionately influence predictions for specific groups.


Step 4: Mitigate Bias

4.1 Rebalance the Dataset

If the dataset is imbalanced, apply techniques like oversampling or undersampling to ensure equal representation.

python
from imblearn.over_sampling import SMOTE

from sklearn.model_selection import train_test_split
X_train_balanced, y_train_balanced = SMOTE().fit_resample(X_train, y_train)

4.2 Modify Feature Weights

Reduce the importance of biased features like Credit_History by transforming or weighting them:

python
X_train['Adjusted_Credit_History'] = X_train['Credit_History'] * 0.5

4.3 Retrain the Model

Train a new model with the adjusted data and compare performance metrics:

python
from sklearn.metrics import accuracy_score

model_balanced = LogisticRegression()
model_balanced.fit(X_train_balanced, y_train_balanced)
y_pred_balanced = model_balanced.predict(X_test)

print("Balanced Model Accuracy:", accuracy_score(y_test, y_pred_balanced))


Step 5: Evaluate Impact

5.1 Compare SHAP Values

Visualize SHAP summary plots for the original and retrained models. The retrained model should show less reliance on biased features.

python
shap_values_balanced = explainer(X_test)
shap.summary_plot(shap_values_balanced, X_test)

5.2 Measure Fairness Metrics

Calculate fairness metrics like Disparate Impact and Equal Opportunity:

python
from aequitas.group import Group
from aequitas.plotting import Plot

# Create fairness metrics
g = Group()
xtab, _ = g.get_crosstabs(data)
metrics = g.compute_group_metrics(xtab)

📊 Visualizing Metrics:

Use fairness visualizations to highlight improvements in the retrained model.


🌟 Real-Life Impact: Ethical AI in Practice

  1. Fair Loan Approvals: Ensures equitable treatment across demographics.
  2. Regulatory Compliance: Avoid legal penalties by adhering to fairness standards.
  3. Building Trust: Customers trust AI decisions when they’re explainable and unbiased.

🔜 What’s Next in This Series?

This blog is Part 5 of the Explainable AI series. In the final blog (Part 6), we’ll:

  • Recap lessons learned from the series.
  • Explore future directions for Explainable AI.
  • Share best practices for applying XAI in various industries.

Stay tuned for the concluding post, and let us know how you’re tackling bias in your AI systems! 🚀

Series Navigation<< Exploring SHAP for Global and Local Interpretability