CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
  • Week 1
    • 1.The ML Lifecycle and the Role of the ML Engineer
    • 2.ML Problem Types and Evaluation Metric Basics
    • 3.AWS ML Stack at a Glance
    • 4.SageMaker Overview: Studio, Training/Inference, Built-in Algorithms
    • 5.Week 1 Comprehensive Review — ML Fundamentals & AWS Stack
  • Week 2
  • Week 3
    • 1.Feature Engineering: The Art of Turning Data into Numbers a Model Can Read
    • 2.SageMaker Data Wrangler: No-Code Data Preparation
    • 3.SageMaker Feature Store: Managing Features as Assets
    • 4.Data Bias·Quality: Clarify, Class Imbalance Handling, Data Split
    • 5.Week 3 Comprehensive Review — Feature Engineering·Data Quality
  • Week 4
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← MLA-C01/Week 3/Day 4

Based on the MLA-C01 exam guide·Report an error

MLA-C01· AssociateWeek 3 · Day 4~28 min read

Day 4 - Data Bias·Quality: Clarify, Class Imbalance Handling, Data Split

📌 Key Takeaways

  • SageMaker Clarify analyzes data bias before training, and prediction bias and explainability (SHAP) after training.
  • The basis of bias analysis is the sensitive attribute (facet). The representative metrics are CI (sample imbalance between groups) and DPL (difference in positive label proportions).
  • Class imbalance is handled with under/oversampling, SMOTE, and class weights. Apply them only to the training set, after splitting.
  • Split into three sets: train/validation/test. Look at the test set only once, at the very end.
  • For imbalanced data, use a stratified split; for time series, a time-based split. With little data, use k-fold cross-validation.

SageMaker Clarify: Bias Detection and Explainability

Even if you engineer features well, if the data itself is skewed to one side, the model learns that bias as it is. Typical examples are a loan approval model making unfavorable decisions for a particular gender, or a fraud detection model missing every single case of fraud that makes up only 0.1%.

SageMaker Clarify does two things.

TimingWhat it analyzesRepresentative output
Before training (pre-training)Imbalance between groups in the data itselfBias metrics such as CI and DPL
After training (post-training)Differences in model predictions between groups, feature contributionsPrediction bias metrics, SHAP values

The core concept is the sensitive attribute (facet). When you specify a column on which fairness should be examined, such as gender, race, or age, Clarify shows with metrics whether the data and predictions between those groups are balanced.

from sagemaker import clarify
 
clarify_processor = clarify.SageMakerClarifyProcessor(
    role=role, instance_count=1, instance_type="ml.m5.xlarge", sagemaker_session=session
)
 
bias_config = clarify.BiasConfig(
    label_values_or_threshold=[1],      # Positive outcome (e.g., loan approved)
    facet_name="gender",                # Sensitive attribute
    facet_values_or_threshold=[0],      # Protected group
)
 
clarify_processor.run_pre_training_bias(   # Pre-training data bias
    data_config=data_config, data_bias_config=bias_config,
)

There are two representative bias metrics.

MetricWhat it measuresWhen high
Class Imbalance (CI)How skewed the sample counts are between groupsOne group has far less data
Difference in Positive Proportions in Labels (DPL)Difference in positive label proportions between groupsOnly one group received favorable outcomes often

Machine learning bias starts with the data. It is the fairness version of "garbage in, garbage out." If past data contains social bias (for example, a low approval rate for a particular group), the model learns it as a "pattern" and automates and amplifies the bias. This is why measuring bias before training matters — if you do not catch the data imbalance before building the model, even the most accurate model makes unfair decisions quickly and in bulk.

Clarify's post-training analysis computes "how much each feature contributed to the prediction" with SHAP (SHapley Additive exPlanations) values. The concept comes from the Shapley value in game theory, which allocates a fair share of the change in prediction by adding and removing features. In regulated industries (finance, healthcare), when you must explain "why was this customer rejected," SHAP-based feature contributions serve as that basis.

Handling Class Imbalance

In classification data, one class being extremely small is class imbalance. Often the side that actually matters is the minority, as with fraud (0.1%), rare diseases, and equipment failures. If left as is, the model gets high accuracy by just guessing the majority class and ignores the minority class.

TechniqueMethodData volumeCaution
UndersamplingReduce the majority classDecreasesMay lose information
Oversampling (duplication)Duplicate the minority classIncreasesRepeats the same points → overfitting risk
SMOTEGenerate synthetic minority samplesIncreasesBetter generalization than plain duplication; synthesizes noise if classes overlap
class weightLarge penalty for minority class mistakesUnchangedDoes not grow the data, saving memory and time
from imblearn.over_sampling import SMOTE
 
# SMOTE: generate synthetic minority samples by interpolation
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X_train, y_train)
print(y_resampled.value_counts())   # The two classes are balanced
[Original]       Majority ●●●●●●●●●●●●●●●●  Minority ▲▲
[Undersampling]  Majority ●●●●             Minority ▲▲     ← Discards the majority (information loss)
[Duplication]    Majority ●●●●●●●●●●●●●●●●  Minority ▲▲▲▲▲▲▲▲  ← Repeats the same points
[SMOTE]          Majority ●●●●●●●●●●●●●●●●  Minority ▲△△▲△△▲△  ← △ are interpolated points between neighbors

⚠️ Trap: Apply resampling only to the training data, after splitting. If you run SMOTE on the full data before splitting, synthesized samples get mixed into the validation and test sets, causing data leakage and overestimated performance. The validation and test sets must reflect the actual production distribution (imbalanced as it is) for the evaluation to be honest.

SMOTE (Synthetic Minority Over-sampling Technique) is a technique proposed in 2002 that creates new synthetic samples by interpolating between minority class samples. Plain duplication places the same point several times so that the model memorizes it (overfitting), but SMOTE creates new points between neighboring samples, smoothing the decision boundary. However, it is not a cure-all, because if the classes overlap in feature space it can synthesize noise.

Handling imbalance is not only about resampling — you also adjust metrics and thresholds.

AxisAction
DataUnder/oversampling, SMOTE
Loss functionclass weight, scale_pos_weight
Evaluation metricPR-AUC, F1, Recall instead of Accuracy
ThresholdAdjust to business cost instead of 0.5

train/validation/test Split

For honest model evaluation you must divide the data into three chunks. Each set has a different role.

SetPurposeRatio (example)Times used
TrainLearn model parameters70%Every epoch
ValidationHyperparameter tuning and model selection15%Many times
TestEvaluate final performance once (never used in training)15%Only once

Look at the test set only once, at the very end. If you start fixing the model after seeing test results, the test set effectively becomes a validation set, and you can no longer measure generalization performance honestly.

from sklearn.model_selection import train_test_split
 
# Step 1: train+val vs test / Step 2: train vs val (stratify keeps the ratio if imbalanced)
X_temp, X_test, y_temp, y_test = train_test_split(
    X, y, test_size=0.15, stratify=y, random_state=42
)
X_train, X_val, y_train, y_val = train_test_split(
    X_temp, y_temp, test_size=0.176, stratify=y_temp, random_state=42
)

stratify=y is a stratified split that keeps the class ratio of each set the same as the original when splitting. With imbalanced data, a random split can leave almost no minority class in some set, so always stratify.

Split method by data nature

Data natureSplit methodReason
General structured, balancedRandom splitSimple and sufficient
Class imbalanceStratified split (stratify)Keeps the class ratio in each set
Time seriesTime-based split (past = train, future = test)Predicting the past from the future leaks
Grouped (several rows from the same user)Split by groupIf the same user is scattered, performance is inflated
Little datak-fold cross-validationReduces variation from the luck of the split
[Correct time series split]
  ──────── train ────────┤── val ──┤── test ──▶ time
                     (train on the past, evaluate on the future)

[Wrong time series split — random]
  ░█░░█░█░░█░█░█░░█░█░  ← Future pieces mixed into train cause look-ahead bias

A random split is forbidden for time series data. This is because future leakage (look-ahead bias) occurs, training on future data to predict the past. For time series, use a time-based split that divides by point in time into "past = train, future = test." Also, when data is small, instead of a single split use k-fold cross-validation, which divides the data k times and looks at the average performance.

Order Mistakes That Create Leakage

A theme repeated throughout this week is "get the order wrong and leakage occurs."

Wrong orderWhat leakageCorrect order
Fit the scaler on everything → splitTransformation leakageSplit → fit on train only
SMOTE on everything → splitSynthesis leakageSplit → SMOTE on train only
Feature selection on everything → splitSelection leakageSplit → select based on train
Using future values as featuresFuture leakagePoint-in-time by Event Time
Keep fixing the model using testEvaluation leakageTest only once, at the end

⚠️ Trap: All five of these produce the same symptom — validation performance is excellent but it collapses once deployed. When the exam gives a passage like "offline metrics were good but performance does not hold up in the live service," you should almost always suspect the leakage family.

At Which Stage Is Bias Handled

Bias mitigation is not a job for a single point. You can intervene at three points — data, training, and prediction — and the exam makes you distinguish "which stage's action is this."

TimingWhat changesExamplePros and cons
Before training (pre-processing)The data itselfResampling, boosting samples per group, removing bias-inducing featuresFundamental, but requires obtaining data
During training (in-processing)The training objectiveclass weight, reflecting fairness constraints in the lossNo need to change the data, complex to implement
After training (post-processing)The prediction resultsAdjusting thresholds per groupQuick to apply, the root cause remains

Clarify's role among these is to measure pre-training data bias and post-training prediction bias. You must distinguish that measurement and mitigation are different jobs.

How to Read Bias Metrics

ObservationInterpretationNext action
CI is largeOne group's samples are markedly fewBoost data, stratified sampling
DPL is largePast decisions favored one groupReview the label creation process, reexamine features
Balanced before training but biased after trainingThe model or features created the biasCheck feature contributions (SHAP)
A feature's SHAP is strongly associated with a sensitive attributePossibly a proxy variableConsider removing that feature

The idea that deleting the sensitive attribute column from the data makes things fair is a common misconception. If a proxy variable strongly associated with the sensitive attribute, such as postal code or occupation, remains, the model still learns the same bias. What is needed is not deleting but an approach of measuring and verifying.

Imbalance and Split Order Summary

① Obtain data
   │
② Split (stratified / time-based / group)          ← First of all!
   │
   ├─ train ─→ ③ Fit transformer  ─→ ④ Resampling (SMOTE etc.)  ─→ ⑤ Train
   │
   ├─ validation ─→ Transformer transform only ─→ Hyperparameter selection
   │
   └─ test ──────→ Transformer transform only ─→ Final one-time evaluation
If you break this orderResult
③ before ②Transformation leakage
④ before ②Synthesis leakage
Repeatedly adjusting ⑤ using testEvaluation leakage
Random ② for time seriesFuture leakage

The Evaluation Set Must Reflect the Production Distribution

  • You may balance the training set, but the validation and test sets must be left as imbalanced as what you actually meet in production.
  • If you balance even the test set, you are measuring performance in a "world where half of all transactions are fraud," which yields numbers unrelated to reality.
  • Threshold decisions must also be made on the production distribution so that the actual precision and recall come out as predicted.

In the next lesson we wrap up Week 3 with a comprehensive review of this week's feature engineering and data quality as a whole — Data Wrangler, Feature Store, and Clarify.

📖 Glossary

  • SageMaker Clarify : A tool that measures bias in data and predictions before and after training and explains the basis for predictions.
  • Sensitive attribute (facet) : A column on which fairness should be examined, such as gender, race, or age. It becomes the reference axis for bias analysis.
  • CI (Class Imbalance) : A bias metric measuring how skewed the sample counts are between sensitive attribute groups.
  • DPL : The difference in positive label proportions between groups. Shows whether past decisions favored one side.
  • SHAP : An explanation metric computed by fairly allocating, in a game-theoretic way, the share each feature contributed to the prediction.
  • Explainability : The property of making it possible for humans to understand why a model predicted what it did.
  • Class imbalance : A state in which one class is extremely small. Makes the accuracy metric meaningless.
  • Undersampling / oversampling : Reducing the majority class / increasing the minority class.
  • SMOTE : An oversampling technique that creates new synthetic samples by interpolating between minority class samples.
  • class weight : A setting that assigns a larger loss when the minority class is wrong so the model pays attention to it.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Before building a loan approval model, you want to measure whether the data is balanced across gender groups and whether the positive label rate differs. What are the appropriate tool and stage?

Question 2

In an imbalanced dataset where fraudulent transactions are only 0.2% of the total, you want to generate synthetic samples of the minority class by interpolation to improve generalization over simple duplication. Which technique is appropriate?

Question 3

You want to apply SMOTE to imbalanced data. What is the correct order to prevent data leakage?

Question 4

In a train/validation/test split, what is the correct principle for using the test set?

Question 5

When splitting heavily imbalanced data into train/test, you want each set to preserve the class proportions of the original. Which method is appropriate?

PreviousSageMaker Feature Store: Managing Features as AssetsWeek 3 · Day 3Next Week 3 Comprehensive Review — Feature Engineering·Data QualityWeek 3 · Day 5

Official references

  • MLA-C01 official exam page(opens in a new tab)
  • Amazon SageMaker AI documentation(opens in a new tab)

On this page

  • Key Takeaways
  • SageMaker Clarify: Bias Detection and Explainability
  • Handling Class Imbalance
  • train/validation/test Split
  • Order Mistakes That Create Leakage
  • At Which Stage Is Bias Handled
  • How to Read Bias Metrics
  • Imbalance and Split Order Summary
  • The Evaluation Set Must Reflect the Production Distribution
  • Glossary
  • Practice Questions