Fairness
Fairness
The property of an AI not producing discriminatory outcomes against particular groups. Various metrics exist, such as statistical parity and equal opportunity.
Fairness
The property of an AI not producing discriminatory outcomes against particular groups. Various metrics exist, such as statistical parity and equal opportunity.
Fairness means that an AI system provides results of equal quality to all users, regardless of sensitive attributes such as gender, race, age, and disability status. If an AI model learns the biases embedded in its training data as they are, it will make predictions that disadvantage certain groups and can amplify social discrimination. For example, if a resume-screening AI learns the gender bias in past hiring data, it may give female applicants lower scores.
Various metrics exist for measuring fairness. Statistical Parity requires that the rate of positive predictions be the same across all groups. Equalized Odds is a criterion that, when actual outcomes are the same, prediction results should be independent of group. Predictive Parity means that among those who received a positive prediction, the proportion who are actually positive should be the same across groups. These metrics are mathematically hard to satisfy simultaneously, so a choice suited to the situation is needed.
Fairness issues must be addressed across the entire ML pipeline, including data collection, preprocessing, model training, and post-processing. At the data collection stage, representative sampling prevents under- or over-representation of particular groups. At the preprocessing stage, removal of sensitive attributes or resampling is applied; at the training stage, fairness constraints are added to the loss function; and at the post-processing stage, group outcomes are balanced by adjusting thresholds.
Fairness is a core element of Trustworthy AI and is treated as important in regulations such as the EU AI Act and Korea's AI Framework Act. With open-source tools such as IBM AI Fairness 360 and Microsoft Fairlearn, you can measure fairness metrics and apply algorithms that mitigate bias. However, because the definition of fairness itself varies with ethical and legal context, technical solutions alone are not enough, and collaboration with domain experts and stakeholders is essential.
This is an example of measuring fairness metrics and mitigating bias using the Fairlearn library.
# pip install fairlearn scikit-learn
from fairlearn.metrics import MetricFrame, selection_rate, false_positive_rate
from fairlearn.reductions import ExponentiatedGradient, DemographicParity
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import fetch_openml
import pandas as pd
# Adult Census 데이터셋 로드 (소득 예측)
data = fetch_openml(data_id=1590, as_frame=True)
X = data.data
y = (data.target == ">50K").astype(int)
# 민감 속성: 성별
sensitive_feature = X["sex"]
# 학습/테스트 분할
X_train, X_test, y_train, y_test, sf_train, sf_test = train_test_split(
X.drop(columns=["sex"]), y, sensitive_feature,
test_size=0.3, random_state=42
)
# 수치형 변환 (간단한 전처리)
X_train = pd.get_dummies(X_train)
X_test = pd.get_dummies(X_test)
X_test = X_test.reindex(columns=X_train.columns, fill_value=0)
# 기본 모델 학습
base_model = GradientBoostingClassifier(n_estimators=50, random_state=42)
base_model.fit(X_train, y_train)
y_pred_base = base_model.predict(X_test)
# 공정성 지표 측정
print("=== 기본 모델 공정성 분석 ===")
mf = MetricFrame(
metrics={"selection_rate": selection_rate, "accuracy": lambda y_true, y_pred: (y_true == y_pred).mean()},
y_true=y_test,
y_pred=y_pred_base,
sensitive_features=sf_test
)
print(f"그룹별 선택률:\n{mf.by_group['selection_rate']}")
print(f"선택률 차이: {mf.difference()['selection_rate']:.4f}")
# 공정성 완화 적용 (Demographic Parity)
mitigator = ExponentiatedGradient(
estimator=GradientBoostingClassifier(n_estimators=50, random_state=42),
constraints=DemographicParity()
)
mitigator.fit(X_train, y_train, sensitive_features=sf_train)
y_pred_fair = mitigator.predict(X_test)
# 완화 후 공정성 비교
print("\n=== 완화 후 공정성 분석 ===")
mf_fair = MetricFrame(
metrics={"selection_rate": selection_rate},
y_true=y_test,
y_pred=y_pred_fair,
sensitive_features=sf_test
)
print(f"그룹별 선택률:\n{mf_fair.by_group['selection_rate']}")
print(f"선택률 차이: {mf_fair.difference()['selection_rate']:.4f}")
# 결과 예시:
# === 기본 모델 공정성 분석 ===
# 그룹별 선택률:
# sex
# Female 0.095
# Male 0.312
# 선택률 차이: 0.2170
#
# === 완화 후 공정성 분석 ===
# 그룹별 선택률:
# sex
# Female 0.182
# Male 0.198
# 선택률 차이: 0.0160