Notes

NearMiss

When one class massively outnumbers the other—think “not fraud” vs “fraud”—a model can look accurate while barely learning the rare cases you care about. NearMiss is a practical way to rebalance the training data by shrinking the majority class, but doing it intelligently rather than at random.

What NearMiss does

NearMiss is an under-sampling method: it removes many majority-class examples so the classifier sees a more balanced dataset. The key idea is distance. It keeps majority points that are “close” to minority points (near the decision boundary) and discards majority points that are far away (easy, redundant examples). That concentrates training on the hard cases where the model must learn to separate classes.

How it chooses which points to keep

NearMiss computes distances (commonly Euclidean in feature space) between majority and minority samples, then selects majority samples using a rule. Common variants include:

  • NearMiss-1: keep majority samples with the smallest average distance to the k nearest minority samples.
  • NearMiss-2: keep majority samples with the smallest average distance to the k farthest minority samples (spreads coverage across minority regions).
  • NearMiss-3: for each minority sample, keep a set of its closest majority neighbors.

Why it matters in real pipelines

On tasks like fraud detection, churn prediction, or disease screening, NearMiss can improve recall for the minority class by forcing the model (e.g., scikit-learn’s LogisticRegression or an SVM) to focus on borderline examples. If features aren’t scaled, distance-based selection can behave badly, so it’s common to standardize first. In Python, it’s widely used via imbalanced-learn:

from imblearn.under_sampling import NearMiss
X_res, y_res = NearMiss(version=1, n_neighbors=3).fit_resample(X, y)

NearMiss is an under-sampling method for imbalanced classification that reduces the majority class by keeping majority examples that are closest to minority examples (based on a distance metric), discarding the rest. Variants (e.g., NearMiss-1/2/3) differ in how “closeness” is computed and which neighbors are used. It matters because it rebalances training data while preserving hard, boundary-defining negatives that strongly influence the learned decision boundary.

Imagine you’re trying to learn what makes an email “spam,” but you have way more normal emails than spam. If you keep all the normal ones, the lesson gets lopsided. NearMiss is a way to fix that by throwing away some of the extra “normal” examples, but not randomly.

Instead, it keeps the normal examples that are closest to the spam ones—the tricky borderline cases that are easiest to confuse. That way, the model practices on the hardest comparisons and learns a sharper dividing line. NearMiss is mainly used when one class is rare (like fraud) and you don’t want the common class to drown it out.