Cleaning data, imbalance and bias

MLA-C01 · Data Preparation for Machine Learning · Updated

Models learn whatever the data says, including its flaws. The exam expects you to name the right fix for a specific data problem and the right AWS tool to apply it.

Missing values and outliers

  • Impute with mean, median or mode, or with a model, or drop rows when few are affected. Median is safer when outliers exist.
  • Add a “was missing” flag when missingness itself carries meaning.
  • Treat outliers with clipping, winsorizing or a log transform. Do not delete them blindly, because they may be real fraud or fault events.
  • Data Wrangler has built-in handling for missing values and outliers. AWS Glue DataBrew offers similar visual recipes.

Encoding and scaling

ProblemTechnique
Unordered categoriesOne-hot encoding
Ordered categoriesOrdinal encoding
Very high cardinalityTarget encoding, hashing or embeddings
Different numeric rangesStandardization (mean 0, variance 1) or min-max scaling
Skewed distributionLog or Box-Cox transform
Free textTokenization, TF-IDF, embeddings

Scaling matters for linear models, k-means, k-NN, SVM and neural networks. Tree-based models such as XGBoost mostly do not need it.

Splitting data and leakage

Split into training, validation and test sets. For time series, split by time and never shuffle across the boundary. Data leakage happens when information from the future or from the label slips into features, or when you fit scalers on the full dataset before splitting. Fit transforms on training data only.

Class imbalance

Options: oversample the minority (random or SMOTE), undersample the majority, use class weights (for example XGBoost scale_pos_weight), or choose metrics such as precision, recall and F1 or AUC-PR rather than accuracy. Accuracy on a 99 to 1 dataset is misleading.

Bias and data quality

Amazon SageMaker AI Clarify measures bias before training (class imbalance, difference in proportions of labels) and after training (for example disparate impact). Run it as a processing job or in Data Wrangler. It also produces SHAP-based feature importance for explainability.

To reduce bias, collect more representative data, rebalance groups and re-run the metrics. Use Amazon SageMaker AI Ground Truth with review workflows to improve label quality.

Sensitive data

Use Amazon Macie to find PII in S3, and mask, hash or remove it before training. Use AWS Glue sensitive data detection for the same goal inside ETL jobs. Encrypt with AWS KMS.

Exam traps

  • Accuracy is the wrong metric for imbalanced classes. Look for precision, recall, F1 or AUC.
  • Scaling is not needed for tree models, but it is for k-means and neural networks.
  • Clarify detects bias and explains predictions. It does not fix bias for you.
  • Fitting a scaler before the train and test split leaks information.
  • Macie finds PII in S3. Comprehend can detect PII in text. They are not interchangeable.

Practice questions

Choose an answer, then review the explanation to check your understanding.

1. A fraud dataset has 0.2 percent positive records and the model predicts "not fraud" for everything. Which technique helps most at the data level?

Show answer

Answer: B.Class imbalance is handled by oversampling the minority class (SMOTE), undersampling the majority class or using class weights.

2. Before training, a team wants to detect whether a gender attribute is unevenly represented in positive labels. Which tool is built for this?

Show answer

Answer: A.SageMaker Clarify computes pre-training bias metrics such as class imbalance and difference in proportions of labels.

3. A categorical column "color" has no natural order and few distinct values. Which encoding is appropriate for a linear model?

Show answer

Answer: B.One-hot encoding avoids implying an order. Ordinal encoding is for ordered categories.

4. A feature has a few extreme values far above the rest and distance-based algorithms perform poorly. What is a reasonable step?

Show answer

Answer: A.Skewed values and outliers distort distance and gradient methods. Log transform, clipping and scaling fix that.

MLA-C01 course outline

All 8 lessons · Course overview
  1. Data storage, ingestion and Feature StoreData Preparation for Machine Learning Free
  2. Cleaning data, imbalance and biasData Preparation for Machine Learning Free
  3. Choosing SageMaker AI algorithms and approachesML Model Development Full access
  4. Training, tuning and overfitting in SageMaker AIML Model Development Full access
  5. Choosing a SageMaker AI inference optionDeployment and Orchestration of ML Workflows Full access
  6. SageMaker AI Pipelines, Model Registry and CI/CDDeployment and Orchestration of ML Workflows Full access
  7. Monitoring models, drift and costML Solution Monitoring, Maintenance, and Security Full access
  8. Securing ML workloads on AWSML Solution Monitoring, Maintenance, and Security Full access