How can I generate binary classification dataset and control the overlapping between 2 classes?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If you want to control how hard a binary classification problem is, the key variable is class overlap. The closer the two classes sit in feature space, or the more label noise you inject, the more ambiguous the decision boundary becomes.
What "Overlap" Really Means
Two classes overlap when points from class 0 and class 1 occupy the same region of feature space. In practice, overlap comes from a few knobs:
- the distance between class centers
- the spread of each class
- the number of clusters per class
- label noise, meaning some points are intentionally assigned the opposite label
If you only want a quick synthetic dataset, sklearn.datasets.make_classification already exposes several of these controls.
Using make_classification
The most direct parameter is class_sep. Larger values move the classes farther apart. Smaller values make the boundary fuzzier. You can also use flip_y to inject label noise.
This example generates two datasets with different overlap levels and plots them:
That is usually enough when you want to benchmark models at several difficulty levels.
When You Need Finer Control
make_classification is convenient, but it does not give you an explicit geometric model of each class. If you want exact control over where each class lives, generate the classes yourself from probability distributions.
A common pattern is to sample each class from a Gaussian distribution with its own mean and covariance matrix:
To increase overlap, move mean_b closer to mean_a or increase the variances in the covariance matrices. To reduce overlap, move the means farther apart or tighten the covariance.
Choosing the Right Control Knob
Different knobs produce different kinds of difficulty.
- '
class_sepmostly changes how far apart the classes are.' - '
flip_yadds mislabeled samples, which simulates annotation noise.' - Larger covariance values create wider clouds and more geometric overlap.
- Multiple clusters per class create nontrivial boundaries even when overall separation looks large.
That matters because not all overlap is the same. A linear model may struggle with intertwined clusters for a different reason than it struggles with noisy labels.
Measure, Do Not Guess
A useful habit is to generate several datasets and compare both a plot and a baseline model score. For example, if logistic regression still gets near-perfect accuracy, your overlap is probably too low for the experiment you want.
Synthetic data is most valuable when the difficulty setting is intentional instead of aesthetic.
Common Pitfalls
- Assuming overlap is controlled only by class means. Variance and label noise matter just as much.
- Using high-dimensional data and then judging overlap only from a two-dimensional projection.
- Forgetting that imbalanced classes and overlapping classes are different problems.
- Making the data unrealistically clean, which can produce misleadingly strong benchmark results.
- Using
flip_yto simulate geometric overlap. It simulates noisy labels, which is related but not identical.
Summary
- Class overlap controls how difficult a binary classification task is.
- '
make_classificationis the quickest way to adjust overlap withclass_sepandflip_y.' - Custom Gaussian sampling gives finer control over means, covariance, and cluster shape.
- Different overlap mechanisms stress models in different ways.
- Plot the data and test a baseline model so your synthetic dataset matches the difficulty you intend.
Related reading
- How can I generate training data on the fly in TensorFlow?
- How can I get biases from a trained model in Keras?
- How can I get the relative importance of features of a logistic regression for a particular prediction?
- How can I implement a weighted cross entropy loss in tensorflow using sparse_softmax_cross_entropy_with_logits
- How can I get a value from a cell of a dataframe?
- How can I get the most frequent 100 numbers out of 4,000,000,000 numbers?
- How can I implement dilated convolution in keras?
- How can I implement incremental training for xgboost?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.