Data bias
Machine learning is changing the world; from recommendation systems to autonomous driving, its applications are everywhere.
However, these intelligent systems are far from perfect; they all share a common **Achilles' heel** (Achilles' Heel) — data bias.
Today, we will delve into this core issue that affects the fairness and accuracy of machine learning models.
What Is Data Bias?
Data bias refers to training data that does not accurately represent real-world situations, causing machine learning models to learn incorrect patterns or make biased predictions. Simply put, it is "garbage in, garbage out" — if the input data is problematic, the output will also be flawed.
Three Main Types of Data Bias
-
Selection bias:This occurs when the data collection process itself has systematic bias. For example, surveying only young people's opinions about a product through social media, while ignoring the elderly who do not use social media.
-
Measurement bias:Errors occur in the data itself during measurement or recording. For example, facial recognition systems were developed primarily using photos of light-skinned people, resulting in lower recognition accuracy for dark-skinned people.
-
Confirmation bias:Researchers or data annotators bring their own subjective biases into the data. For example, in sentiment analysis tasks, annotators may interpret the sentiment of a text based on their own cultural background, ignoring expressions from other cultures.

How Does Data Bias Affect Machine Learning Models?
Model Performance Degradation
When a model performs well on training data but poorly on real-world data, data bias is likely to blame.
Example
Simulating the Impact of Data Bias on Model Performance
import
numpy
as
np
from
sklearn.
model_selection
import
train_test_split
from
sklearn.
linear_model
import
LogisticRegression
from
sklearn.
metrics
import
accuracy_score
np.
random
.
seed
(
42
)
n_samples
=
1000
Real-world data
X_real
=
np.
random
.
uniform
(
5
,
5
,
n_samples
)
.
reshape
(
1
,
1
)
y_real
=
(
X_real.
flatten
(
)
0
)
.
astype
(
int
)
Biased data: 90% from X > 0, 10% from X < 0
mask_pos
=
X_real.
flatten
(
)
0
mask_neg
=
X_real.
flatten
(
)
<=
0
X_pos
=
X_real
[
mask_pos
]
y_pos
=
y_real
[
mask_pos
]
X_neg
=
X_real
[
mask_neg
]
y_neg
=
y_real
[
mask_neg
]
Sample only a small number of negative examples
neg_sample_size
=
int
(
0.1
len
(
X_pos
)
)
neg_indices
=
np.
random
.
choice
(
len
(
X_neg
)
,
neg_sample_size
,
replace
=
False
)
X_biased
=
np.
vstack
(
[
X_pos
,
X_neg
[
neg_indices
]
]
)
y_biased
=
np.
hstack
(
[
y_pos
,
y_neg
[
neg_indices
]
]
)
Split into training / test sets
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X_biased
,
y_biased
,
test_size
=
0.2
,
random_state
=
42
)
Train the model
model
=
LogisticRegression
(
)
model.
fit
(
X_train
,
y_train
)
Training set evaluation
train_pred
=
model.
predict
(
X_train
)
train_acc
=
accuracy_score
(
y_train
,
train_pred
)
(
f
"Training accuracy: {train_acc:.2%}"
)
Real-world evaluation
real_pred
=
model.
predict
(
X_real
)
real_acc
=
accuracy_score
(
y_real
,
real_pred
)
(
f
"Real-world accuracy: {real_acc:.2%}"
)
(
"Conclusion: The training set performs well, but due to distribution bias, real-world performance drops significantly"
)
The output is:
训练准确率: 99.08%
真实世界准确率: 96.00%
结论:训练集表现良好,但由于分布偏差,真实世界性能显著下降
Fairness Issues
Data bias can lead models to produce discriminatory results against certain groups. For example, a hiring algorithm trained primarily on historical data of male employees may be biased against female job applicants.
Poor Generalization
The model cannot adapt to new, unseen data scenarios because the training data does not cover sufficiently diverse situations.
Common Sources of Data Bias
Problems in the Data Collection Stage
| Problem Type | Specific Manifestation | Example |
|---|---|---|
| Sampling Bias | The data sample cannot represent the population | Collect autonomous driving data only in urban areas, ignoring rural roads |
| Temporal Bias | Data is outdated or not timely | Using e-commerce data from 2010 to predict consumption trends in 2023 |
| Survivorship Bias | Focusing only on "surviving" data points | Studying only data from successful companies, ignoring the lessons from failed ones |
Problems in the Data Labeling Stage
Example
Simulating the Impact of Labeling Bias
import
pandas
as
pd
Create a simulated dataset
data
=
{
'text'
:
[
'This product is really great, I love it!'
,
'Not sure if this is good to use'
,
'Absolutely do not buy this junk product'
,
'It's okay, just average'
,
'Highly recommended, great value for money'
]
,
Assume the annotator has their own bias: label positive reviews as 1, everything else as 0
'biased_label'
:
[
1
,
0
,
0
,
0
,
1
]
,
Biased labels
'true_label'
:
[
1
,
0.5
,
0
,
0.5
,
1
]
True continuous sentiment scores
}
df
=
pd.
DataFrame
(
data
)
(
"Labeling bias example:"
)
(
df
)
(
"\nProblem: The annotator incorrectly labeled neutral reviews as negative!"
)
The output is:
标注偏差示例:
text biased_label true_label
0 这个产品真的很棒,我非常喜欢! 1 1.0
1 不太确定这个好不好用 0 0.5
2 绝对不要买这个垃圾产品 0 0.0
3 还行吧,一般般 0 0.5
4 超级推荐,物超所值 1 1.0
问题:标注者将中性评价错误地标注为负面!
Problems in the Data Preprocessing Stage
- Improper handling of outliers
- Biased feature selection
- Inappropriate data normalization methods
How Can Data Bias Be Detected?
1. Statistical Analysis of Data
Check the distribution of different groups in the dataset.
Example
Check the distribution of different groups in the dataset
import
matplotlib.
pyplot
as
plt
-------------------------- Set Chinese font start --------------------------
plt.
rcParams
[
'font.sans-serif'
]
=
[
Windows first
'SimHei'
,
'Microsoft YaHei'
,
macOS first
'PingFang SC'
,
'Heiti TC'
,
Linux first
'WenQuanYi Micro Hei'
,
'DejaVu Sans'
]
Fix the issue of minus signs displaying as squares
plt.
rcParams
[
'axes.unicode_minus'
]
=
False
-------------------------- Set Chinese font end --------------------------
Simulate demographic data
groups
=
[
'Group A'
,
'Group B'
,
'Group C'
,
'Group D'
]
population_percent
=
[
40
,
30
,
20
,
10
]
True population proportions
dataset_percent
=
[
70
,
20
,
8
,
2
]
Proportions in the dataset
fig
,
(
ax1
,
ax2
)
=
plt.
subplots
(
1
,
2
,
figsize
=
(
12
,
5
)
)
True population distribution
ax1.
pie
(
population_percent
,
labels
=
groups
,
autopct
=
'%1.1f%%'
)
ax1.
set_title
(
'Real-world population distribution'
)
Dataset distribution
ax2.
pie
(
dataset_percent
,
labels
=
groups
,
autopct
=
'%1.1f%%'
)
ax2.
set_title
(
'Group distribution in the dataset'
)
plt.
tight_layout
(
)
plt.
show
(
)
(
"Detection result: Group A is overrepresented in the dataset, while Group D is underrepresented!"
)

2. Model Performance Disparity Analysis
Compare model performance across different subgroups.
3. Fairness Metric Calculation
Use statistical metrics to quantify the degree of model fairness.
Strategies for Addressing Data Bias
Data-Level Solutions
1. Improving Data Collection Strategies
- Active sampling: Consciously collect underrepresented data
- Data augmentation: Increase data diversity through technical means
- Multiple data sources: Integrate data from different sources
2. Data Preprocessing Techniques
Example
Use resampling techniques to balance the dataset
import
numpy
as
np
from
sklearn.
utils
import
resample
np.
random
.
seed
(
42
)
1. Construct imbalanced data (one-dimensional feature + label)
X_majority
=
np.
random
.
normal
(
0
,
1
,
900
)
.
reshape
(
1
,
1
)
y_majority
=
np.
zeros
(
900
,
dtype
=
int
)
X_minority
=
np.
random
.
normal
(
2
,
1
,
100
)
.
reshape
(
1
,
1
)
y_minority
=
np.
ones
(
100
,
dtype
=
int
)
(
f
"Before resampling:"
)
(
f
"Majority class: {len(X_majority)}"
)
(
f
"Minority class: {len(X_minority)}"
)
2. Upsample the minority class
X_minority_upsampled
,
y_minority_upsampled
=
resample
(
X_minority
,
y_minority
,
replace
=
True
,
n_samples
=
len
(
X_majority
)
,
random_state
=
42
)
3. Merge the balanced datasets
X_balanced
=
np.
vstack
(
[
X_majority
,
X_minority_upsampled
]
)
y_balanced
=
np.
hstack
(
[
y_majority
,
y_minority_upsampled
]
)
(
f
"\nAfter resampling:"
)
(
f
"Majority class: {np.sum(y_balanced == 0)}"
)
(
f
"Minority class: {np.sum(y_balanced == 1)}"
)
(
"\nConclusion: The sample counts are now fully balanced"
)
Output:
重采样前:
多数类: 900
少数类: 100
After resampling:
多数类: 900
少数类: 900
结论:样本数量已完全平衡
Algorithm-Level Solutions
-
Fairness constraints: Add fairness constraints during model training.
-
Adversarial debiasing: Use adversarial learning techniques to reduce bias in the model.
-
Post-processing methods: Adjust model predictions to improve fairness.
Practical Exercise: Building an Unbiased Classifier
Let us learn, through a complete example, how to handle data bias throughout the entire process from data collection to model evaluation.
Example
Complete Example: Machine Learning Pipeline for Handling Data Bias (Engineering Grade)
import
numpy
as
np
import
pandas
as
pd
from
sklearn.
datasets
import
make_classification
from
sklearn.
model_selection
import
train_test_split
from
sklearn.
ensemble
import
RandomForestClassifier
from
sklearn.
metrics
import
classification_report
,
confusion_matrix
from
imblearn.
over_sampling
import
SMOTE
import
warnings
warnings
.
filterwarnings
(
"ignore"
,
category
=
FutureWarning
)
1. Generate simulated imbalanced data
X
,
y
=
make_classification
(
n_samples
=
2000
,
n_features
=
10
,
n_informative
=
8
,
n_redundant
=
2
,
n_clusters_per_class
=
1
,
weights
=
[
0.9
,
0.1
]
,
Deliberately introduce class bias
flip_y
=
0
,
random_state
=
42
)
Convert to DataFrame for easier analysis
feature_names
=
[
f
'feature_{i}'
for
i
in
range
(
X.
shape
[
1
]
)
]
df
=
pd.
DataFrame
(
X
,
columns
=
feature_names
)
df
[
'target'
]
=
y
(
"=== Data Bias Analysis ==="
)
(
f
"Dataset shape: {df.shape}"
)
(
"Class distribution:"
)
(
df
[
'target'
]
.
value_counts
(
)
)
(
f
"Minority class proportion: {df['target'].value_counts(normalize=True)[1]:.2%}"
)
2. Data Splitting (Maintain Class Proportions to Prevent Secondary Bias)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.3
,
random_state
=
42
,
stratify
=
y
)
(
"\n=== Training / Test Set Distribution ==="
)
(
"Training set:"
,
np.
bincount
(
y_train
)
)
(
"Test set:"
,
np.
bincount
(
y_test
)
)
3. Use SMOTE to Address Training Set Bias
(
"\n=== Handling Data Bias (SMOTE) ==="
)
smote
=
SMOTE
(
random_state
=
42
)
X_train_balanced
,
y_train_balanced
=
smote.
fit_resample
(
X_train
,
y_train
)
(
"Before SMOTE:"
,
np.
bincount
(
y_train
)
)
(
"After SMOTE:"
,
np.
bincount
(
y_train_balanced
)
)
4. Model training
(
"\n=== Model Training ==="
)
Baseline model: no bias handling
model_imbalanced
=
RandomForestClassifier
(
n_estimators
=
200
,
random_state
=
42
)
model_imbalanced.
fit
(
X_train
,
y_train
)
Bias-handling model: trained after SMOTE
model_balanced
=
RandomForestClassifier
(
n_estimators
=
200
,
random_state
=
42
)
model_balanced.
fit
(
X_train_balanced
,
y_train_balanced
)
5. Model Evaluation
(
"\n=== Model Evaluation (Test Set) ==="
)
(
"\n[Unhandled Bias] Classification Report"
)
y_pred_imbalanced
=
model_imbalanced.
predict
(
X_test
)
(
classification_report
(
y_test
,
y_pred_imbalanced
,
digits
=
4
)
)
(
"Confusion matrix:"
)
(
confusion_matrix
(
y_test
,
y_pred_imbalanced
)
)
(
"\n[After SMOTE] Classification Report"
)
y_pred_balanced
=
model_balanced.
predict
(
X_test
)
(
classification_report
(
y_test
,
y_pred_balanced
,
digits
=
4
)
)
(
"Confusion matrix:"
)
(
confusion_matrix
(
y_test
,
y_pred_balanced
)
)
(
"\nConclusion:\n"
"1. Without handling bias, the model's Recall for the minority class is extremely low\n"
"2. SMOTE significantly improves minority class Recall and F1\n"
"3. Accuracy is not a reliable metric for imbalanced data"
)
Output result:
=== 数据偏差分析 ===
数据集形状: (2000, 11)
Category distribution:
target
0 1800
1 200
Name: count, dtype: int64
少数类占比: 10.00%
=== 训练 / 测试集分布 ===
训练集: [1260 140]
测试集: [540 60]
=== 处理数据偏差(SMOTE)===
SMOTE 前: [1260 140]
SMOTE 后: [1260 1260]
=== 模型训练 ===
=== 模型评估(测试集)===
【未处理偏差】分类报告
precision recall f1-score support
0 0.9872 1.0000 0.9936 540
1 1.0000 0.8833 0.9381 60
accuracy 0.9883 600
macro avg 0.9936 0.9417 0.9658 600
weighted avg 0.9885 0.9883 0.9880 600
Confusion matrix:
[[540 0]
[ 7 53]]
【SMOTE 处理后】分类报告
precision recall f1-score support
0 0.9944 0.9944 0.9944 540
1 0.9500 0.9500 0.9500 60
accuracy 0.9900 600
macro avg 0.9722 0.9722 0.9722 600
weighted avg 0.9900 0.9900 0.9900 600
Confusion matrix:
[[537 3]
[ 3 57]]
结论:
1. 不处理偏差时,模型对少数类 Recall 极低
2. SMOTE 显著提升少数类 Recall 与 F1
3. Accuracy 不是不平衡数据的可靠指标
Industry Best Practices for Data Bias Management
1. Establish a Data Governance Framework
- Develop standard processes for data collection and labeling
- Regularly audit data quality
- Establish data bias detection mechanisms
2. Build Diverse Teams
- Ensure diversity in the data science team
- Include domain experts and ethicists
- Conduct regular bias awareness training
3. Transparency and Interpretability
- Document data sources and processing procedures
- Provide explanations for model decisions
- Disclose model performance across different groups
4. Continuous Monitoring and Updating
- Regularly evaluate model performance in the real world
- Establish feedback mechanisms to collect user reports
- Update the model promptly to adapt to changes
Summary and outlook
Data bias is an inevitable challenge in machine learning, but through systematic detection and handling methods, we can significantly mitigate its impact. The key is to recognize:
- Data bias is everywhere: Almost all real-world datasets contain some form of bias
- Early detection is crucial: Considering bias issues early in a project is the least costly approach
- Combining technology + process: Purely technical solutions are not enough; they must be combined with process and management
- Continuous improvement process: Handling data bias is an ongoing process, not a one-time task
As demands for machine learning fairness and accountability continue to grow, effectively managing data bias will become a core skill for every data scientist and machine learning engineer. Remember, a good machine learning system must have not only high accuracy, but also high fairness and reliability.