Bias detection with Fairlearn, explainability with SHAP, audit logging — EU AI Act and NIST AI RMF compliance.
Responsible AI and AI Governance
# AI Governance Framework
# 1. Bias Detection and Mitigation
from fairlearn.metrics import MetricFrame, selection_rate
from sklearn.metrics import accuracy_score
metric_frame = MetricFrame(
metrics={"accuracy": accuracy_score,
"selection_rate": selection_rate},
y_true=y_test,
y_pred=y_pred,
sensitive_features=gender_feature # protected attribute
)
print(metric_frame.by_group) # accuracy per demographic group
# Flag if disparity > acceptable threshold
# 2. Model Explainability (XAI)
import shap
explainer = shap.Explainer(model)
shap_values = explainer(X_test)
shap.plots.beeswarm(shap_values) # feature importance
shap.plots.waterfall(shap_values[0]) # single prediction explanation
# 3. AI Audit Log
log.info("ai_decision",
model_id="credit-scorer-v3",
input_hash=hash(str(features)),
decision="approved",
confidence=0.87,
top_features=shap_top_3,
user_id=applicant_id)
# 4. EU AI Act compliance
# High-risk systems: credit scoring, hiring, medical, law enforcement
# Requirements: human oversight, explainability, bias testing
# NIST AI RMF: Govern, Map, Measure, Manage