* More Developers Docs: The AI Bias Auditor is a Python-based framework that identifies and evaluates potential biases in machine learning (ML) models. It provides a structured mechanism to analyze protected features (e.g., gender, race) and their relationship to model performance metrics, such as prediction accuracy. By quantifying fairness gaps and classifying outcomes as biased or unbiased, this tool enables responsible and ethical AI development.
The BiasAuditor class:
This implementation is highly valuable for:
The BiasAuditor framework defines bias using two key metrics:
Bias Threshold: Bias is classified when:
fairness_gap = max(group_stats) - min(group_stats) if fairness_gap > 0.1: feature is biased
For each protected feature, the bias report includes:
Example Bias Report:
json
{
"gender": {
"group_stats": {"male": 0.85, "female": 0.78},
"fairness_gap": 0.07,
"is_biased": false
},
"race": {
"group_stats": {"white": 0.90, "black": 0.75, "asian": 0.89},
"fairness_gap": 0.15,
"is_biased": true
}
}
The BiasAuditor class requires two key inputs:
Class Constructor:
python
class BiasAuditor:
def __init__(self, protected_features: List[str], outcome_feature: str):
"""
:param protected_features: List of protected features (e.g., gender, race).
:param outcome_feature: Target feature measuring fairness (e.g., accuracy).
"""
Core Method: evaluate_bias()
python
def evaluate_bias(self, data: pd.DataFrame) -> dict:
"""
Evaluate bias within the dataset.
:param data: Pandas DataFrame containing the dataset.
:return: Bias analysis report as a dictionary.
"""
report = {}
for feature in self.protected_features:
group_stats = data.groupby(feature)[self.outcome_feature].mean()
fairness_gap = group_stats.max() - group_stats.min()
report[feature] = {
"group_stats": group_stats.to_dict(),
"fairness_gap": fairness_gap,
"is_biased": fairness_gap > 0.1 # Define 0.1 as the bias threshold
}
return report
Dataset Example:
| gender | race | prediction_accuracy |
| ———- | ——— | ——————— |
| male | white | 0.9 |
| female | black | 0.7 |
| male | black | 0.8 |
| female | white | 0.85 |
| male | asian | 0.92 |
| female | asian | 0.9 |
Code Example:
python
import pandas as pd
data = pd.DataFrame({
"gender": ["male", "female", "male", "female", "male", "female"],
"race": ["white", "black", "black", "white", "asian", "asian"],
"prediction_accuracy": [0.9, 0.7, 0.8, 0.85, 0.92, 0.9]
})
auditor = BiasAuditor(protected_features=["gender", "race"], outcome_feature="prediction_accuracy")
bias_report = auditor.evaluate_bias(data)
print("Bias Report:", bias_report)
Output:
Bias Report: { 'gender': { 'group_stats': {'male': 0.873333, 'female': 0.816667}, 'fairness_gap': 0.056666, 'is_biased': False }, 'race': { 'group_stats': {'white': 0.875, 'black': 0.75, 'asian': 0.91}, 'fairness_gap': 0.16, 'is_biased': True } }
Modify the bias threshold using a derived class:
python
class CustomBiasAuditor(BiasAuditor):
def __init__(self, protected_features, outcome_feature, bias_threshold=0.1):
super().__init__(protected_features, outcome_feature)
self.bias_threshold = bias_threshold
def evaluate_bias(self, data):
report = super().evaluate_bias(data)
for feature, details in report.items():
details["is_biased"] = details["fairness_gap"] > self.bias_threshold
return report
# Custom threshold example
auditor = CustomBiasAuditor(protected_features=["gender"], outcome_feature="prediction_accuracy", bias_threshold=0.05)
bias_report = auditor.evaluate_bias(data)
print("Bias Report with Custom Threshold:", bias_report)
Add visual insights by plotting group statistics and fairness gaps:
python
import matplotlib.pyplot as plt
def plot_bias_report(bias_report):
for feature, details in bias_report.items():
group_stats = details["group_stats"]
fairness_gap = details["fairness_gap"]
# Bar chart of group statistics
plt.bar(group_stats.keys(), group_stats.values(), color="skyblue")
plt.title(f"Bias Analysis: {feature}")
plt.xlabel("Groups")
plt.ylabel("Outcome Metric")
plt.axhline(y=max(group_stats.values()) - fairness_gap, color="red", linestyle="--", label="Fairness Gap")
plt.legend()
plt.show()
plot_bias_report(bias_report)
Use a loop to audit multiple datasets efficiently:
python
data_files = ["dataset1.csv", "dataset2.csv", "dataset3.csv"]
for file in data_files:
data = pd.read_csv(file)
auditor = BiasAuditor(protected_features=["gender", "race"], outcome_feature="prediction_accuracy")
bias_report = auditor.evaluate_bias(data)
print(f"Bias Report for {file}:\n", bias_report)
Audit machine learning models during validation:
python from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier import pandas as pd
# Simulated dataset
data = pd.DataFrame({
"gender": ["male", "female", "male", "female", "male", "female"],
"income": [50000, 45000, 60000, 48000, 52000, 46000],
"race": ["white", "black", "black", "white", "asian", "asian"],
"loan_approval": [1, 0, 0, 1, 1, 1]
})
X = data[["gender", "income", "race"]] y = data["loan_approval"]
# Model training
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) clf = RandomForestClassifier() clf.fit(pd.get_dummies(X_train), y_train)
# Evaluate bias on predictions
X_test["prediction_accuracy"] = clf.predict(pd.get_dummies(X_test))
auditor = BiasAuditor(protected_features=["gender", "race"], outcome_feature="prediction_accuracy")
bias_report = auditor.evaluate_bias(X_test)
print("Bias Report:", bias_report)
1. Responsible AI Development:
Automatically audit ML models for fairness during deployment and validation.
2. Compliance:
Analyze compliance with fairness guidelines in line with AI ethics standards.
3. Business Insights:
Detect unintended biases in decision-making systems, such as loan approvals or hiring tools.
1. Validate Dataset: Confirm protected and outcome features are present before running an audit. 2. Custom Thresholds: Adjust fairness thresholds to fit domain-specific fairness guidelines. 3. Visualize Results: Use visualization tools to make bias reports more interpretable.
The AI Bias Auditor empowers users to evaluate the fairness of ML models in a structured and interpretable way. Its customizable threshold, extensibility, and integration into ML pipelines make it ideal for building responsible AI systems. By identifying biases early in the development cycle, this tool promotes transparency and accountability in AI.