CLOUD CONCEPT

Class Resampling

Class resampling changes how frequently examples from each target class appear during training to reduce the effects of class imbalance.

Cloud Concepts Hub →

What is Class Resampling

Class resampling addresses unequal class representation by removing examples, repeating examples, or generating additional training examples. It can improve learning for rare outcomes in numeric, text, and image datasets, but it must preserve label validity and avoid contaminating evaluation data.

Class resampling changes the class distribution seen by a learning algorithm. In an imbalanced classification dataset, common classes can dominate training while rare but important classes contribute relatively little. For example, a transaction dataset containing 99,000 legitimate transactions and 1,000 fraudulent transactions allows a model that always predicts legitimate to achieve 99% accuracy. Resampling can make fraud examples more influential, but success must be measured through useful predictions rather than a balanced training dataset alone.

Random undersampling removes examples from majority classes. It reduces training time and storage requirements, but it can discard important variations and difficult boundary cases. Random oversampling repeats minority-class examples, preserving the original majority data while increasing training work and potentially encouraging memorization. Synthetic oversampling creates additional examples rather than simply duplicating existing ones. Hybrid strategies combine these approaches. A 50:50 distribution is not a universal goal; the sampling ratio should be selected using validation results and the operational costs of missed detections and false alarms.

For numeric tabular data, SMOTE generates synthetic minority examples by interpolating between neighboring minority observations. Its distance calculations require meaningful feature scales, so scaling parameters must be learned from training data only. Ordinary SMOTE is unsuitable for directly interpolating categorical identifiers or category codes. Mixed-type methods such as SMOTENC can handle categorical features, but generated records still require checks for valid combinations and business constraints. Synthetic samples can amplify mislabeled observations or bridge regions that should remain separate.

For text classification, engineers can oversample documents belonging to rare intent or moderation categories, or use class-aware sampling in the training data loader. Label-preserving paraphrasing can add variety, but replacements that change negation, sentiment, or intent invalidate labels. Applying SMOTE to vector representations does not create valid documents and may produce vectors that do not represent realistic language. Treat such feature-space techniques as experiments requiring validation, not as automatic substitutes for collecting representative text.

For image classification, a sampler can present rare-class images more often while applying appropriate random transformations during training. Cropping, rotation, and color changes must preserve the target label; rotating a digit or removing a small defect can change the task meaning. Augmentation is not inherently class balancing: applying identical augmentation policies and sampling frequencies to every class can leave the imbalance unchanged. Related images from the same patient, device, or capture sequence should remain in the same data partition.

Split data before resampling. Keep validation and test data representative of the intended deployment population, and resample only the training partition. Within cross-validation, fit preprocessing and resampling separately inside each training fold. Otherwise, duplicated examples or synthetic samples derived from held-out observations can leak information and inflate reported performance. Use stratified splits when appropriate, but honor temporal ordering and entity boundaries when those constraints better represent deployment.

In cloud workflows, managed processing jobs can profile class counts, validate labels, create training partitions, and execute resampling code. Training jobs can instead perform class-aware sampling dynamically, avoiding large stored copies of repeated text or images. Persist sampling ratios, random seeds, dataset versions, and transformation settings so that experiments can be reproduced. Distributed data loaders also need explicit sampler configuration so that sharding and repeated examples do not unintentionally distort the intended class mix.

Evaluate per-class precision and recall, macro F1, confusion matrices, and precision-recall curves rather than accuracy alone. Resampling changes the class prevalence observed during training, so predicted probabilities may need calibration using untouched, representative validation data. Class-weighted losses and decision-threshold adjustments are related alternatives, not resampling techniques; combining them requires testing to avoid overcompensation. Finally, balancing target labels does not automatically resolve demographic bias, missing population coverage, or poor labels. Audit performance across relevant groups as well as across classes.

Key aspects of Class Resampling

Effective class resampling changes training exposure while preserving valid examples, trustworthy evaluation, reproducibility, and deployment-relevant performance.

Controlled class exposure

Adjust the frequency with which each class contributes training examples through undersampling, oversampling, or a combination. Tune the ratio against operational metrics rather than assuming equal class counts are optimal.

Leakage-safe evaluation

Split before resampling and apply resampling only within training partitions or cross-validation training folds. Keep evaluation data representative of deployment and respect entity and time boundaries.

Modality-aware sample generation

Use numeric interpolation only where feature distances and generated values are meaningful. For text and images, favor validated label-preserving transformations and class-aware sampling rather than assuming synthetic examples are automatically valid.

Information and compute trade-offs

Undersampling can remove valuable majority-class patterns, while oversampling increases training work and can encourage overfitting. Dynamic sampling can avoid storing many duplicate records.

Imbalance-aware measurement

Measure minority-class precision and recall alongside macro metrics and business costs. Check probability calibration on representative data because altered training prevalence can affect predicted probabilities.

Reproducibility and bias checks

Record dataset versions, seeds, class counts, sampling ratios, and augmentation settings. Verify that improved class-level metrics do not conceal worse performance for important demographic or operational subgroups.

Cloud services for Class Resampling

AWS Glue or Amazon EMR can prepare large datasets, check label distributions, and create leakage-safe training and evaluation partitions. Amazon SageMaker AI can then run more specialized resampling code in processing jobs or perform class-aware sampling directly during model training. Choose Glue or EMR according to the existing data platform rather than requiring both. Keep evaluation datasets unchanged, record sampling configurations with each experiment, and compare candidate models using minority-class metrics and representative validation data. Dynamic sampling is often preferable to materializing repeated image or text files, while persisted resampled tables can simplify reuse for tabular experiments.

Amazon SageMaker image Amazon SageMaker

SageMaker Processing can run custom preprocessing and resampling code, including compatible Python libraries packaged in a processing container. SageMaker training jobs can use framework data loaders for class-aware sampling and compare sampling strategies through tracked experiments.

Open Amazon SageMaker →
AWS Glue image AWS Glue

AWS Glue ETL jobs can calculate class counts, validate records, and implement class-dependent sampling with Spark code. Engineers must explicitly define the sampling logic and keep evaluation partitions outside the resampling workflow.

Open AWS Glue →
Amazon EMR image Amazon EMR

Amazon EMR can run distributed Spark jobs for profiling and resampling large labeled datasets. Class-dependent sampling and custom synthetic-example generation require explicit implementation, with attention to shuffles, partition skew, and reproducibility.

Open Amazon EMR →