machine learning
sklearn
data splitting
training set
test set

How to split data on balanced training set and test set on sklearn

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In machine learning, preparing your dataset is a crucial step before building any model. One common task is splitting the dataset into training and testing sets. However, in cases where your dataset is imbalanced—meaning some classes are underrepresented compared to others—it's important to maintain this imbalance proportionately in both your training and test sets. The scikit-learn library (sklearn), a powerful Python library for machine learning, provides efficient utilities for this task.

Understanding Imbalanced Datasets

An imbalanced dataset is one where the distribution of classes is not uniform. For example, in a disease detection dataset, there might be many more healthy samples compared to diseased ones. Training a model on an imbalanced dataset can lead to a biased model that is overly optimistic about predicting the majority class, thereby underperforming on the minority class.

Why Balanced Splitting is Important

Balanced splitting ensures that the model:

  • Learns to identify patterns related to the minority class.
  • Can be evaluated properly during testing across both the majority and minority classes.
  • Remains robust and generalizes well on unseen data.

Splitting Data with sklearn

The train_test_split function from the sklearn.model_selection module is widely used for dataset splitting. When dealing with imbalanced datasets, the stratify parameter comes in handy.

Steps to Split Data

  1. Loading Data: Use any dataset you'd like, ensuring it's loaded into a pandas dataframe or NumPy array.
  2. Identify Features and Labels: Separate your feature set (X) from your labels (y).
  3. Invoke train_test_split with stratify: Pass in the y labels to the stratify parameter.

Code Example

Below is a basic example using a synthetic dataset:

python
1from sklearn.datasets import make_classification
2from sklearn.model_selection import train_test_split
3import numpy as np
4
5# Create a synthetic dataset
6X, y = make_classification(n_samples=1000, n_features=10, n_classes=2,
7                           n_clusters_per_class=1, weights=[0.9, 0.1],
8                           flip_y=0, random_state=42)
9
10# Display original class distribution
11original_distribution = np.bincount(y)
12print(f"Original distribution: Class 0 = {original_distribution[0]}, Class 1 = {original_distribution[1]}")
13
14# Split with stratification
15X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
16
17# Display class distribution in train and test sets
18train_distribution = np.bincount(y_train)
19test_distribution = np.bincount(y_test)
20
21print(f"Training distribution: Class 0 = {train_distribution[0]}, Class 1 = {train_distribution[1]}")
22print(f"Testing distribution: Class 0 = {test_distribution[0]}, Class 1 = {test_distribution[1]}")

Explanation

  • make_classification: Generates a synthetic dataset with an imbalance (90% Class 0 and 10% Class 1).
  • train_test_split: The stratify=y parameter ensures the class distribution in the training and test sets remains proportional to the original dataset.

Key Points

Here is a summary of the steps and their outcomes:

StepFunction/MethodOutcome
Load Datasetdatasets.load_*Loads data into applicable structures such as NumPy arrays or pandas dataframes.
Separate Features and LabelsN/AX for features, y for labels derived from the dataset.
Split with Stratificationtrain_test_splitEnsures the training and testing set maintain the class distribution from the original dataset.
Verify Distributionnp.bincountUtilized to display class counts in the training and testing datasets.

Additional Considerations

Handling Multi-Class Imbalance

The stratify parameter in train_test_split can be used with multi-class datasets as well. Just pass the y array, and sklearn will handle the distribution proportionality across all classes.

Advanced Techniques

For more complex datasets, consider:

  • Resampling Techniques: Implementing oversampling (e.g., SMOTE) or undersampling methods.
  • Cost-Sensitive Training: Using algorithms that consider class weights to focus more on the minority class during training.

Conclusion

Balanced splitting of datasets is a vital step, particularly in scenarios with imbalanced classes. Leveraging the stratify parameter in sklearn's train_test_split can ensure that your analyses remain robust and meaningful by retaining the imbalance ratio. This practice not only aids in the effective training of machine learning models but also ensures balanced evaluation, making your model applicable and reliable in real-world scenarios.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design