By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Data quality checks are the first line of defense in any ML pipeline—garbage in, garbage out. This topic covers how to detect and handle missing values, outliers, and class imbalance in training datasets. These issues distort model performance, increase bias, and lead to unreliable predictions.
Real-world scenario:A fintech company builds a fraud detection model using transaction logs. The dataset has: - Missing values (e.g., null merchant_category in 15% of records).- Outliers (e.g., a single $10M transaction skewing the distribution).- Class imbalance (only 0.1% of transactions are fraudulent).Without proper checks, the model may overfit to noise, ignore rare fraud cases, or fail in production due to unseen edge cases.
merchant_category
Amazon SageMaker Data Wrangler AWS’s no-code/low-code tool for exploratory data analysis (EDA), cleaning, and feature engineering. Integrates with SageMaker Pipelines for automated workflows. Best for quick prototyping and visualizing data quality issues (e.g., missing values, outliers).
AWS Glue DataBrew A serverless data preparation tool for cleaning and normalizing datasets. Unlike Data Wrangler, it’s batch-focused and integrates with Glue ETL jobs. Best for large-scale, scheduled data cleaning (e.g., nightly fraud dataset updates).
Amazon SageMaker Clarify Detects bias and data quality issues (e.g., class imbalance, feature skew) before training. Generates bias reports and feature importance metrics. Critical for compliance-heavy use cases (e.g., lending, hiring models).
Amazon SageMaker Processing A managed compute environment for running custom data processing scripts (Python/R) at scale. Use for custom outlier detection (e.g., IQR, Z-score) or synthetic minority oversampling (SMOTE) for class imbalance.
AWS Lambda + Amazon EventBridge For real-time data quality checks in streaming pipelines (e.g., Kinesis). Example: Trigger a Lambda function to flag missing values in incoming transaction data before it hits a fraud model.
Flagging (add a binary column indicating missingness).
Outliers Data points far from the rest (e.g., a $10M transaction in a dataset where 99% are <$1K). Can skew models (e.g., linear regression) or hide fraud patterns. Detection methods:
ML-based (Isolation Forest, DBSCAN).
Class Imbalance When one class dominates the dataset (e.g., 99.9% non-fraud, 0.1% fraud). Causes models to ignore the minority class. Fixes:
Algorithm-level (class weights, anomaly detection models like XGBoost with scale_pos_weight).
scale_pos_weight
Data Drift When statistical properties of data change over time (e.g., transaction amounts increase due to inflation). Detected via SageMaker Model Monitor or Evidently AI. Requires retraining or adjusting thresholds.
Feature Store (SageMaker Feature Store) A centralized repository for storing and serving features. Ensures consistency between training and inference (e.g., same imputation logic for missing values). Critical for real-time ML (e.g., recommendation systems).
Goal: Identify missing values, outliers, and class imbalance before training.Tools: SageMaker Data Wrangler, Pandas, SageMaker Clarify.
Steps:1. Load data into SageMaker Data Wrangler (from S3, Redshift, or Athena).2. Run automated EDA (Data Wrangler generates histograms, missing value heatmaps, and outlier plots).3. Use SageMaker Clarify to generate a bias report (checks for class imbalance, feature skew).4. Manually inspect with Pandas: ```python # Missing values df.isnull().sum()
# Outliers (IQR method) Q1 = df.quantile(0.25) Q3 = df.quantile(0.75) IQR = Q3 - Q1 outliers = df[(df < (Q1 - 1.5 * IQR)) | (df > (Q3 + 1.5 * IQR))]
# Class imbalance df['target'].value_counts(normalize=True) ```
Goal: Decide whether to impute, drop, or flag missing data.Tools: Data Wrangler, Glue DataBrew, SageMaker Processing.
Steps:1. For numerical columns: - Impute with mean/median (if <10% missing). - Use KNN imputation (if missingness is correlated with other features). - Flag missingness (add a binary column, e.g., is_missing_merchant_category).2. For categorical columns: - Impute with mode (most frequent category). - Create a "Missing" category (if missingness is meaningful).3. Automate in Data Wrangler or Glue DataBrew: - Apply pre-built transformations (e.g., "Fill missing values with median"). - Export the cleaned dataset to S3.
is_missing_merchant_category
Goal: Decide whether to remove, cap, or keep outliers.Tools: SageMaker Processing, Pandas, Scikit-learn.
Steps:1. Detect outliers using: - Z-score (for normally distributed data). - IQR (for skewed data). - Isolation Forest (for high-dimensional data).2. Decide on treatment: - Remove (if outliers are errors, e.g., negative age). - Cap (e.g., set all values above 99th percentile to the 99th percentile). - Keep (if outliers are meaningful, e.g., fraud transactions).3. Implement in SageMaker Processing: python from sklearn.ensemble import IsolationForest clf = IsolationForest(contamination=0.01) # Adjust contamination outliers = clf.fit_predict(df[['amount']]) df = df[outliers == 1] # Keep inliers
python from sklearn.ensemble import IsolationForest clf = IsolationForest(contamination=0.01) # Adjust contamination outliers = clf.fit_predict(df[['amount']]) df = df[outliers == 1] # Keep inliers
Goal: Ensure the model doesn’t ignore the minority class.Tools: SageMaker Processing, Imbalanced-learn (SMOTE), XGBoost.
Steps:1. Check imbalance ratio (e.g., 99:1 non-fraud to fraud).2. Choose a method: - Resampling: - Oversample minority class (duplicate fraud cases). - Undersample majority class (randomly drop non-fraud cases). - Synthetic data: - SMOTE (generate synthetic fraud cases). - ADASYN (focus on "hard" minority cases). - Algorithm-level: - Class weights (e.g., scale_pos_weight=100 in XGBoost). - Anomaly detection (Isolation Forest, One-Class SVM).3. Implement in SageMaker Processing: python from imblearn.over_sampling import SMOTE smote = SMOTE(sampling_strategy=0.5) # Balance to 50:50 X_res, y_res = smote.fit_resample(X, y)
scale_pos_weight=100
python from imblearn.over_sampling import SMOTE smote = SMOTE(sampling_strategy=0.5) # Balance to 50:50 X_res, y_res = smote.fit_resample(X, y)
Goal: Ensure consistency between training and inference.Tools: SageMaker Pipelines, Lambda, EventBridge.
Steps:1. Create a SageMaker Pipeline with a Processing Step for data quality checks.2. Add a Lambda function to validate incoming data in real-time (e.g., check for missing values in Kinesis streams).3. Use SageMaker Model Monitor to detect data drift in production.4. Store cleaned data in SageMaker Feature Store for reuse.
NaN
income
Exam trap: If the question mentions real-time data cleaning, neither is ideal—use Lambda + Kinesis.
SageMaker Clarify vs. SageMaker Model Monitor:
A fintech company trains a fraud detection model on a dataset where 0.1% of transactions are fraudulent. The model performs well on the majority class but fails to detect fraud. What is the most likely cause, and what two fixes would you recommend?
Answer:- Cause: Class imbalance (model ignores the minority fraud class).- Fixes: 1. Resampling (oversample fraud cases or undersample non-fraud cases). 2. Algorithm-level (use scale_pos_weight in XGBoost or class weights in logistic regression).
A data scientist uses SageMaker Data Wrangler to clean a dataset with missing values in a categorical column. They impute with the mode, but the model’s performance degrades in production. What is the most likely reason, and what should they do instead?
Answer:- Reason: Missingness was not random (e.g., certain demographics were more likely to skip the field). Imputing with mode introduced bias.- Fix: Create a "Missing" category or use multiple imputation (MICE) to account for non-random missingness.
A retail company deploys a recommendation model that uses customer purchase history. After 6 months, the model’s accuracy drops significantly. SageMaker Model Monitor detects feature drift in average_order_value. What two actions should the team take?
average_order_value
Answer:1. Retrain the model with recent data to adapt to the new distribution.2. Investigate the root cause (e.g., inflation, new product lines) and adjust business rules if needed (e.g., cap extreme values).
⚠️ Never drop >5% of data without justification.
Outliers:
Z-score (normal data), IQR (skewed data), Isolation Forest (high-dimensional).
Class imbalance:
⚠️ SMOTE fails on high-dimensional data—reduce dimensions first.
SageMaker Data Wrangler:
⚠️ Not for production pipelines—use SageMaker Processing instead.
Glue DataBrew:
⚠️ No real-time support—use Lambda + Kinesis for streaming.
SageMaker Clarify:
⚠️ Does not monitor production models—use Model Monitor.
SageMaker Model Monitor:
⚠️ Requires a baseline dataset (e.g., training data).
Feature Store:
⚠️ Not a replacement for ETL—clean data before storing.
Data drift:
Fix: Retrain model or adjust thresholds.
Exam traps:
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.