Most bias in AI systems originates from biased training data. This chapter covers techniques for detecting bias in datasets before they poison your models. 弘益人間 - Clean data leads to fair AI.
Data bias is systematic error in the data collection, sampling, or labeling process that results in misrepresentation of the true population. Unlike random noise, data bias creates consistent patterns that ML models will learn and amplify.
Selection bias occurs when the data collection process systematically excludes or underrepresents certain groups. This is one of the most common and pernicious forms of data bias.
# Detect Selection Bias
import pandas as pd
import numpy as np
def detect_selection_bias(df, population_stats, protected_attr):
"""
Compare dataset distribution to known population statistics
"""
dataset_dist = df[protected_attr].value_counts(normalize=True)
print("Selection Bias Analysis")
print("=" * 60)
print(f"\n{'Group':<20} {'Dataset %':<15} {'Population %':<15} {'Bias'}")
print("-" * 60)
for group in population_stats.keys():
dataset_pct = dataset_dist.get(group, 0) * 100
population_pct = population_stats[group] * 100
bias = dataset_pct - population_pct
status = "⚠️" if abs(bias) > 5 else "✓"
print(f"{group:<20} {dataset_pct:<15.2f} {population_pct:<15.2f} {bias:+.2f}% {status}")
return dataset_dist
# Example
population = {'Group_A': 0.60, 'Group_B': 0.30, 'Group_C': 0.10}
# detect_selection_bias(dataset, population, 'demographic')
Measurement bias arises when features are systematically measured differently across groups or when measurement tools have different accuracy for different populations.
# Detect Measurement Bias
def analyze_measurement_quality(df, feature, protected_attr):
"""
Analyze measurement consistency across groups
"""
groups = df[protected_attr].unique()
print(f"Measurement Quality Analysis: {feature}")
print("=" * 60)
for group in groups:
group_data = df[df[protected_attr] == group][feature]
# Check for patterns suggesting measurement issues
missing_rate = group_data.isna().sum() / len(group_data)
outlier_rate = len(group_data[np.abs(group_data - group_data.mean()) > 3*group_data.std()]) / len(group_data)
print(f"\n{group}:")
print(f" Missing rate: {missing_rate:.3f}")
print(f" Outlier rate: {outlier_rate:.3f}")
print(f" Mean: {group_data.mean():.3f}")
print(f" Std: {group_data.std():.3f}")
if missing_rate > 0.05:
print(f" ⚠️ High missing rate may indicate measurement bias")
Label bias occurs when the labeling process is inconsistent or biased. This is particularly problematic in supervised learning where labels are the ground truth.
# Detect Label Bias
def detect_label_bias(df, label_col, protected_attr, feature_cols):
"""
Look for cases where labels differ despite similar features
"""
from sklearn.neighbors import NearestNeighbors
groups = df[protected_attr].unique()
# Find similar instances across groups
X = df[feature_cols].values
nbrs = NearestNeighbors(n_neighbors=5).fit(X)
disagreements = 0
total_pairs = 0
for i in range(len(df)):
distances, indices = nbrs.kneighbors([X[i]])
for idx in indices[0][1:]: # Skip self
if df.iloc[i][protected_attr] != df.iloc[idx][protected_attr]:
total_pairs += 1
if df.iloc[i][label_col] != df.iloc[idx][label_col]:
disagreements += 1
disagreement_rate = disagreements / total_pairs if total_pairs > 0 else 0
print(f"Label disagreement rate for similar cross-group pairs: {disagreement_rate:.3f}")
if disagreement_rate > 0.3:
print("⚠️ High disagreement suggests potential label bias")
return disagreement_rate
Comprehensive analysis of how data is distributed across protected groups.
# Comprehensive Dataset Audit
def audit_dataset(df, protected_attributes, target):
"""
Perform comprehensive bias audit of dataset
"""
print("DATASET BIAS AUDIT REPORT")
print("=" * 70)
# 1. Sample Size Analysis
print("\n1. SAMPLE SIZE BY GROUP")
print("-" * 70)
for attr in protected_attributes:
counts = df[attr].value_counts()
print(f"\n{attr}:")
for group, count in counts.items():
pct = count / len(df) * 100
warning = " ⚠️ UNDERREPRESENTED" if pct < 10 else ""
print(f" {group}: {count} ({pct:.2f}%){warning}")
# 2. Label Distribution by Group
print("\n2. LABEL DISTRIBUTION BY GROUP")
print("-" * 70)
for attr in protected_attributes:
print(f"\n{attr}:")
cross_tab = pd.crosstab(df[attr], df[target], normalize='index') * 100
print(cross_tab.round(2))
# Check for significant disparities
positive_rates = cross_tab[1] if 1 in cross_tab.columns else cross_tab.iloc[:, 1]
if positive_rates.max() - positive_rates.min() > 20:
print(" ⚠️ Significant label imbalance detected across groups")
# 3. Missing Data Analysis
print("\n3. MISSING DATA BY GROUP")
print("-" * 70)
for attr in protected_attributes:
print(f"\n{attr}:")
for group in df[attr].unique():
group_df = df[df[attr] == group]
missing_pct = (group_df.isna().sum() / len(group_df) * 100).mean()
print(f" {group}: {missing_pct:.2f}% average missing")
# 4. Feature Correlation with Protected Attributes
print("\n4. FEATURE CORRELATION WITH PROTECTED ATTRIBUTES")
print("-" * 70)
numeric_cols = df.select_dtypes(include=[np.number]).columns
for attr in protected_attributes:
if attr in numeric_cols:
continue
print(f"\n{attr}:")
# One-hot encode for correlation
encoded = pd.get_dummies(df[attr], prefix=attr)
for col in numeric_cols:
if col != target:
corr = df[col].corr(encoded.iloc[:, 0])
if abs(corr) > 0.3:
print(f" {col}: {corr:.3f} ⚠️ HIGH CORRELATION")
print("\n" + "=" * 70)
print("AUDIT COMPLETE")
print("Review warnings above for potential bias issues")
print("=" * 70)
Even when protected attributes are excluded, proxy features can enable discrimination.
# Detect Proxy Features
def detect_proxy_features(df, protected_attr, feature_cols, threshold=0.5):
"""
Identify features that may serve as proxies for protected attributes
"""
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
print("PROXY FEATURE DETECTION")
print("=" * 60)
X = df[feature_cols]
y = df[protected_attr]
# Train model to predict protected attribute from features
clf = RandomForestClassifier(n_estimators=100, random_state=42)
scores = cross_val_score(clf, X, y, cv=5)
mean_score = scores.mean()
print(f"Can predict {protected_attr} from features: {mean_score:.3f} accuracy")
if mean_score > threshold:
print("⚠️ WARNING: Features may contain proxies for protected attribute")
# Get feature importance
clf.fit(X, y)
importance = pd.DataFrame({
'feature': feature_cols,
'importance': clf.feature_importances_
}).sort_values('importance', ascending=False)
print("\nMost predictive features (potential proxies):")
print(importance.head(10))
return mean_score
# Example usage
# detect_proxy_features(df, 'race', ['zipcode', 'income', 'education'])
Data collected over time may reflect changing biases or become outdated.
# Temporal Bias Analysis
def analyze_temporal_bias(df, timestamp_col, protected_attr, target):
"""
Analyze how bias evolves over time
"""
df['year'] = pd.to_datetime(df[timestamp_col]).dt.year
print("TEMPORAL BIAS ANALYSIS")
print("=" * 60)
for group in df[protected_attr].unique():
group_df = df[df[protected_attr] == group]
yearly_stats = group_df.groupby('year').agg({
target: ['mean', 'count']
}).round(3)
print(f"\n{group}:")
print(yearly_stats)
# Check for trends
print("\n⚠️ Look for:")
print(" - Changing label distributions over time")
print(" - Varying representation across years")
print(" - Sudden shifts that may indicate data drift")
Bias often compounds across multiple protected attributes. Intersectional analysis examines combinations of attributes.
# Intersectional Bias Analysis
def intersectional_analysis(df, protected_attrs, target):
"""
Analyze bias across intersections of multiple protected attributes
"""
print("INTERSECTIONAL BIAS ANALYSIS")
print("=" * 70)
# Create intersection groups
df['intersection'] = df[protected_attrs].apply(
lambda x: '_'.join(x.astype(str)), axis=1
)
results = df.groupby('intersection').agg({
target: ['mean', 'count']
}).round(3)
results.columns = ['positive_rate', 'count']
results = results.sort_values('positive_rate')
print(results)
# Identify most disadvantaged intersections
min_rate = results['positive_rate'].min()
max_rate = results['positive_rate'].max()
disparity = max_rate - min_rate
print(f"\nPositive rate range: {min_rate:.3f} to {max_rate:.3f}")
print(f"Disparity: {disparity:.3f}")
if disparity > 0.2:
print("⚠️ Significant intersectional bias detected")
print("\nMost disadvantaged groups:")
print(results.head(3))
return results
# Generate Comprehensive Data Bias Report
def generate_data_bias_report(df, protected_attrs, target, feature_cols):
"""
Generate comprehensive data bias assessment report
"""
report = {
'timestamp': pd.Timestamp.now(),
'total_samples': len(df),
'issues': []
}
print("\n" + "=" * 70)
print("DATA BIAS DETECTION REPORT")
print("=" * 70)
print(f"Generated: {report['timestamp']}")
print(f"Total Samples: {report['total_samples']}")
# Run all checks
audit_dataset(df, protected_attrs, target)
for attr in protected_attrs:
proxy_score = detect_proxy_features(df, attr, feature_cols)
if proxy_score > 0.5:
report['issues'].append(f"Proxy features detected for {attr}")
# Intersectional analysis
if len(protected_attrs) > 1:
intersectional_analysis(df, protected_attrs, target)
print("\n" + "=" * 70)
print("RECOMMENDATIONS")
print("=" * 70)
print("1. Address underrepresented groups through additional data collection")
print("2. Review and standardize labeling procedures")
print("3. Investigate high-correlation features for proxy discrimination")
print("4. Consider stratified sampling for future data collection")
print("5. Implement continuous monitoring of data quality")
print("\n弘익人間 - Fair data is the foundation of fair AI")
print("=" * 70)
return report
Korea operates its industrial ecosystem and standardization system through the following core infrastructure. Korea Top 5 Groups: Samsung, Hyundai Motor, LG, SK, Lotte. Each group operates standardization committees and ISO/IEC TC Korean secretariats. Samsung Electronics (semiconductors, displays, home appliances, telecom)·Hyundai Motor (automobiles, mobility)·LG Electronics (home appliances, displays, OLED)·SK hynix (memory)·LG Energy Solution·Samsung SDI (batteries)·POSCO Future M (materials)·Hyundai Mobis (parts). Korean IT Big Tech: NAVER (search, cloud, AI HyperCLOVA)·Kakao (messenger, payment, mobility, banking)·Coupang (e-commerce, logistics)·Karrot Market·Toss·Woowa Brothers. Korea Telcos: SK Telecom·KT·LG U+. 5G·5G dedicated networks·B2B cloud·AI businesses operating. Korea Top 7 Research Universities: Seoul National University·KAIST·POSTECH·Yonsei University·Korea University·UNIST·DGIST·GIST. All serve as standardization R&D bases and ISO/IEC/IEEE Korean chairs. Korea Government-affiliated National Research Institutes (26): KIST, KAERI, KIMM, KIER, KFRI, KRICT, KRIBB, KARI, KASI, KIGAM, KICT, KISTI, KETI, ETRI, NIMS, KIMS, KISDI, KOTRA, STEPI, KOEN, KICCE, KIET, KIPF, KIHASA, KICJ, KLRI. Korea Industrial Complexes / Tech Valleys: Pangyo Techno Valley·Dongtan·Gwanggyo·Songdo IBD·Yeouido·Gangnam·Sihwa·Banwol·Gumi·Ulsan·Changwon·Geoje·Yeosu·Onsan·Cheongju·Iksan·Gwangyang·POSCO Gwangyang Steel Mill·Asan Bay·Seosan·Songdo·Incheon Airport·Sejong·Cheongna·Geomdan. Korea Trade and Finance Infrastructure: Korea International Trade Association (KITA)·Korea Trade-Investment Promotion Agency (KOTRA)·Export-Import Bank of Korea (KEXIM)·Bank of Korea·Kookmin Bank·Shinhan·Hana·Woori·NH Nonghyup·IBK Industrial Bank·SC First Bank·Citi Bank Korea·HSBC Korea·DBS Korea — 14 Korean major banks and foreign banks. Korea K-POP / K-Content: HYBE·SM·YG·JYP 4 major entertainment companies·CJ ENM·tvN·MBC·KBS·SBS·EBS·YTN·Yonhap News TV·JTBC Korean broadcasting·NETFLIX Korea·Disney Plus·TVING·Wavve·Watcha·Coupang Play. Korea Gaming Industry: Nexon·NCsoft·Krafton·Netmarble·Kakao Games·Pearl Abyss·Com2uS·Gamevil·NHN·Smilegate·Webzen. Korea Automotive / Battery: Hyundai Motor·Kia·Genesis·LG Energy Solution·Samsung SDI·SK On·POSCO Future M·EcoPro·L&F battery cathode material suppliers. Korea Semiconductor: Samsung Electronics (HBM3E·HBM4)·SK hynix (HBM3E 12-Hi)·DB HiTek·SK siltron·SK Enpulse·Dongjin Semichem·Seoul Semiconductor·Simmtech·Samsung Display·LG Display.
Korea operates a comprehensive standards governance system through inter-ministerial cooperation. National Standards Council (under Prime Minister's Office, per Framework Act on National Standards Article 5) coordinates KATS (Korean Agency for Technology and Standards), MFDS (Ministry of Food and Drug Safety), MOTIE (Ministry of Trade, Industry and Energy), MSIT (Ministry of Science and ICT), MOIS (Ministry of the Interior and Safety), MOE (Ministry of Environment), MOHW (Ministry of Health and Welfare), MND (Ministry of National Defense), MCST (Ministry of Culture, Sports and Tourism), MOFA (Ministry of Foreign Affairs), MOJ (Ministry of Justice), and FSC (Financial Services Commission). Accreditation and Testing: KOLAS (Korea Laboratory Accreditation Scheme) accredits 800+ testing laboratories. KAS (Korea Accreditation System) accredits 50+ certification bodies. KTC (Korea Testing Certification), KTR (Korea Testing & Research Institute), KTL (Korea Testing Laboratory), and KCL (Korea Conformity Laboratories) provide conformance testing. Telecom and Cyber: KCC (Korea Communications Commission), KCA (Korea Communications Agency), TTA (Telecommunications Technology Association), IITP (Institute for Information & Communications Technology Planning & Evaluation), NIPA (National IT Industry Promotion Agency), KISA (Korea Internet & Security Agency), KCMVP (Korea Cryptographic Module Validation Program), NIS (National Intelligence Service), NSR (National Security Research Institute), and NCSC (National Cyber Security Center). National R&D Centers: KIST, ETRI, KAIST, Seoul National University, Yonsei University, Korea University, POSTECH, UNIST, GIST, DGIST, KISTI, KIER, KIMM, KRICT, KFRI, KRIBB. International Standards Cooperation: ISO TC/SC Korean secretariats, IEC TC/SC Korean secretariats, ITU-T Study Group Korean chairs, 3GPP RAN/SA Korean chairs, IEEE 802 Korean chairs, W3C Korea office, OASIS Korea office, IETF Korea cooperation, OECD CSTP, UN ESCAP, APEC SCSC Korean cooperation. Korean Industrial Standards (KS) Catalog: KS X (Information) 25,000+, KS A (Basic) 15,000+, KS B (Machinery) 25,000+, KS C (Electrical) 18,000+, KS D (Metallurgy) 12,000+, KS E (Mining) 5,000+, KS F (Construction) 18,000+, KS H (Food) 8,000+, KS I (Environment) 5,000+, KS J (Biology) 3,000+, KS K (Textile) 15,000+, KS L (Ceramics) 7,000+, KS M (Chemistry) 12,000+, KS P (Medical) 5,000+, KS Q (Quality Mgmt) 4,000+, KS R (Transport) 12,000+, KS S (Service) 3,000+, KS T (Packaging) 4,000+, KS V (Shipbuilding) 5,000+, KS W (Aerospace) 3,000+ — totaling 220,000+ Korean Industrial Standards. Key Acts: Personal Information Protection Act (Act 19234, effective Sept 15, 2024), Electronic Government Act, Electronic Signature Act, Act on Promotion of Information and Communications Network Utilization and Information Protection, Information and Communications Infrastructure Protection Act, Data Industry Act, Public Data Act, AI Framework Act (Act 20212, effective July 2026), Industrial Technology Innovation Promotion Act, Framework Act on Science and Technology — 70+ Korean standardization-related laws.
Korea operates digital transformation through a comprehensive governance system. Digital Government: Digital Platform Government Committee (established September 2022, under the President)·Ministry of the Interior and Safety Digital Government Bureau·e-Government Support Center·Gov.kr·National Citizen Service·KDIS (Korea Digital Information Society)·NIA (National Information Society Agency)·MOIS (Ministry of the Interior and Safety). K-DNS Infrastructure: Korea Internet & Security Agency (KISA) Korea Internet Center·KISA DNS Root Server·KRNIC (Korea Network Information Center)·BGP Korea·National Cyber Security Center (NCSC)·KCC (Korea Communications Commission)·MSIT (Ministry of Science and ICT)·NIA·NIPA. Korean Cloud Infrastructure: KT Cloud·NAVER Cloud (NCloud)·Samsung SDS Cloud·LG U+ Cloud·NHN Cloud·Kakao Enterprise Cloud·SK Telecom Cloud·KISA Cloud Security Assurance Program (CSAP)·KCMVP-validated cloud·ISMS-P (Information Security & Personal Information Management System). Korean Security Certifications: KISA ISMS-P certification·KCMVP (Korean Cryptographic Module Validation Program)·NIS (National Intelligence Service) "National Cryptographic Technology Operation Standards"·NCSC "National Cyber Security Strategy 2024-2028"·CC (Common Criteria) Korean evaluation bodies·EAL4·EAL5·KS X ISO/IEC 15408·19790·24759 Korean Profile. Korean Data Standards: NIA AI Hub·National Data Standardization Committee·Statistics Korea (KOSTAT)·MyData 4 Designated Combination Specialists (Samsung SDS, KICI, KOSTAT, KFTC)·National Institute of Korean Language·National Law Information Center·National Spatial Information Platform·National Spatial Data Center·Korean Spatial Information Standards. Finance and Fintech Standards: FSC (Financial Services Commission)·FSS (Financial Supervisory Service)·FIU (Financial Intelligence Unit)·BOK (Bank of Korea)·FSEC (Financial Security Institute)·KFTC (Korea Financial Telecommunications)·KSD (Korea Securities Depository)·KRX (Korea Exchange) 8-agency cooperation. 5G/6G Communications Infrastructure: 5G subscribers 35 million (2024)·5G base stations 350,000·6G commercialization target 2028·5G dedicated networks 16 operators·6G Acceleration Council (MSIT, 2024). K-Content: KOCCA (Korea Creative Content Agency)·MCST (Ministry of Culture, Sports and Tourism)·KCA (Korea Communications Agency)·Korea Culture Information Service Agency·Korean Film Archive·Korea Publishing Industry Promotion Agency. Data 3 Acts (Personal Information Protection Act·Credit Information Act·Telecommunications Network Act, 2020 enforcement)·Data Industry Act (2021)·Public Data Act (2013)·AI Framework Act (2026)·Digital Platform Government Framework Act (2024 proposed) — Korea digital transformation core legislation.