Stratified sampling is a dataset splitting technique in machine learning where the data is divided in such a way that the same proportion of classes is maintained in both training and testing sets.
In simple terms:
👉 Stratified sampling ensures that each split of the data has a balanced representation of all classes.
1. Why Stratified Sampling Is Used
When splitting data randomly (train-test split), there is a risk that:
- One class may be overrepresented in training data
- Another class may be underrepresented in testing data
This becomes a serious problem in imbalanced datasets, such as:
- Fraud detection (very few fraud cases)
- Medical diagnosis (rare diseases)
- Spam detection
👉 Stratified sampling avoids this issue by preserving class distribution.
2. How Stratified Sampling Works
Instead of splitting the dataset randomly, stratified sampling:
- Groups data based on class labels
- Splits each group separately
- Combines the splits while maintaining original proportions
Example
Suppose a dataset has:
After stratified split:
Training set:
Testing set:
👉 Both sets reflect the original distribution.
3. Why It Is Important for Imbalanced Data
In imbalanced datasets, random splitting can cause problems like:
- Testing set missing minority class samples
- Model not learning minority patterns properly
- Overestimating model performance
Stratified sampling ensures:
- Minority class is present in both sets
- Model learns from all classes properly
- Evaluation is more reliable
4. Stratified Sampling vs Random Sampling
Random Sampling
- Data is split randomly
- Class distribution may become uneven
- Risk of biased evaluation
Stratified Sampling
- Maintains class proportions
- Produces more reliable results
- Preferred for classification tasks
5. Real-World Example
In a fraud detection dataset:
- 98% normal transactions
- 2% fraud transactions
Without stratification:
- Test set might have 0% fraud cases → useless evaluation
With stratification:
- Both training and testing sets include fraud cases
👉 This ensures the model is properly evaluated.
6. Advantages of Stratified Sampling
- Maintains class balance
- Improves model evaluation accuracy
- Works well with imbalanced datasets
- Reduces sampling bias
- Provides more reliable performance metrics
7. When to Use It
Stratified sampling is commonly used in:
- Classification problems
- Medical diagnosis models
- Fraud detection systems
- Any dataset with uneven class distribution
Conclusion
Stratified sampling is a dataset splitting technique that ensures each subset of data maintains the same class distribution as the original dataset. It is especially important for imbalanced datasets because it prevents biased training and unreliable evaluation. By keeping both training and testing sets balanced, stratified sampling helps machine learning models learn better and provides more accurate performance measurement.