Inferensys

Glossary

NearMiss

A family of under-sampling algorithms that selects majority class examples based on their average distance to the nearest minority class instances, keeping those closest to the decision boundary.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
UNDER-SAMPLING ALGORITHM

What is NearMiss?

NearMiss is a family of heuristic under-sampling algorithms that selects majority class instances based on their average distance to the nearest minority class examples, retaining only those closest to the decision boundary.

NearMiss is a data-level method for handling class imbalance that reduces the size of the majority class by selecting a subset of its instances. Unlike random under-sampling, which discards data arbitrarily, NearMiss uses a distance metric to keep majority examples that are most informative for classification. The algorithm calculates the average distance between each majority instance and its k nearest minority class neighbors, then selects the majority instances with the smallest distances, effectively focusing the model on the ambiguous region where classes overlap.

The NearMiss family includes three distinct heuristics. NearMiss-1 selects majority instances with the smallest average distance to the three closest minority examples. NearMiss-2 selects majority instances with the smallest average distance to the three farthest minority examples, providing broader coverage. NearMiss-3 is a two-step process that selects a fixed number of the closest majority instances for each minority example. This technique is often paired with SMOTE or Edited Nearest Neighbors in hybrid resampling pipelines to clarify the decision boundary.

UNDER-SAMPLING STRATEGIES

NearMiss Variants

The NearMiss family comprises three distinct heuristic algorithms that select majority class instances based on their average distance to minority class examples, each designed to preserve different characteristics of the original data distribution.

01

NearMiss-1

Selects majority class samples whose average distance to the three closest minority class instances is the smallest.

  • Mechanism: For each majority sample, compute the mean distance to its k nearest minority neighbors. Keep the m majority samples with the lowest mean distances.
  • Effect: Retains majority examples that are closest to the minority class, effectively focusing the classifier on the most ambiguous region near the decision boundary.
  • Risk: Can amplify noise if the minority class contains outliers, as the algorithm will aggressively select majority points near those outliers.
  • Use Case: When the minority class is compact and well-defined, and you want the model to learn a tight boundary.
02

NearMiss-2

Selects majority class samples whose average distance to the three farthest minority class instances is the smallest.

  • Mechanism: For each majority sample, compute the mean distance to its k farthest minority neighbors. Keep the m majority samples with the lowest mean distances.
  • Effect: Retains majority examples that are closest to the periphery of the minority class distribution, providing a broader, more diverse selection of majority points around the minority class.
  • Advantage: Less sensitive to minority class outliers compared to NearMiss-1, as it considers the farthest minority neighbors rather than the nearest.
  • Use Case: When the minority class has scattered sub-clusters and you need wider coverage around each minority instance.
03

NearMiss-3

A two-stage algorithm that, for each minority class instance, selects a fixed number of the closest majority class neighbors.

  • Mechanism: For each minority sample, keep its m nearest majority neighbors. This directly balances the dataset by ensuring each minority example is surrounded by a controlled number of majority examples.
  • Effect: Guarantees that every minority instance is retained and paired with a local neighborhood of majority points, preserving the local density structure around each rare event.
  • Contrast: Unlike NearMiss-1 and NearMiss-2, which select a global count of majority samples, NearMiss-3 makes a per-minority-instance selection, inherently linking the sampling to the minority class distribution.
  • Use Case: When preserving the local topology around every fraud example is critical for the downstream classifier.
04

Distance Metric Selection

The choice of distance metric fundamentally alters which majority instances are selected by any NearMiss variant.

  • Euclidean Distance: Default metric. Sensitive to feature scaling; requires normalization. Works well for continuous, normally distributed features.
  • Manhattan Distance: Less sensitive to outliers in high-dimensional spaces. Preferable when features are sparse or have heavy-tailed distributions.
  • Mahalanobis Distance: Accounts for feature covariance, making it scale-invariant. Critical when transaction features are highly correlated.
  • Cosine Similarity: Focuses on angular separation rather than magnitude. Useful when the direction of a transaction vector matters more than its absolute value.
  • Best Practice: Always standardize features before applying any NearMiss variant, and experiment with multiple distance metrics on a held-out validation set.
05

Heuristic Limitations

NearMiss algorithms rely on distance-based heuristics that make implicit assumptions about the data geometry, which can fail in practice.

  • Curse of Dimensionality: In high-dimensional feature spaces common in fraud detection, pairwise distances become less meaningful. NearMiss may select uninformative majority samples.
  • Noisy Minority Instances: NearMiss-1 is particularly vulnerable to selecting majority points near mislabeled or anomalous minority examples, degrading the decision boundary.
  • No Statistical Guarantee: Unlike cost-sensitive learning or ensemble methods, NearMiss provides no theoretical guarantee on preserving the true underlying distribution.
  • Mitigation: Combine NearMiss with Tomek Links or Edited Nearest Neighbors as a post-cleaning step to remove ambiguous majority instances retained near the boundary.
06

Integration with Ensemble Methods

NearMiss can be embedded within bagging and boosting frameworks to recover information lost during under-sampling.

  • Bagging + NearMiss: Train multiple base classifiers, each on a different NearMiss-1 under-sampled subset of the majority class. Aggregate predictions via majority voting to reduce variance.
  • Boosting + NearMiss: At each boosting iteration, apply NearMiss-2 to the majority class before training the next weak learner. This forces subsequent learners to focus on increasingly difficult boundary regions.
  • Hybrid Pipeline: Apply NearMiss-3 for initial balancing, then use SMOTE to over-sample the minority class, followed by a gradient-boosted tree model with scale_pos_weight tuning.
  • Library Support: The imbalanced-learn library provides NearMiss as a scikit-learn compatible sampler, enabling seamless integration into Pipeline objects with RandomUnderSampler or ensemble wrappers like BalancedBaggingClassifier.
UNDER-SAMPLING COMPARISON

NearMiss vs. Other Under-Sampling Methods

A comparison of NearMiss variants against other common under-sampling techniques for handling imbalanced datasets in fraud detection.

FeatureNearMiss-1NearMiss-2NearMiss-3Random Under-SamplingTomek LinksEdited Nearest Neighbors

Selection Strategy

Selects majority samples with smallest average distance to 3 nearest minority samples

Selects majority samples with smallest average distance to 3 farthest minority samples

Selects majority samples closest to decision boundary using 2-step nearest neighbor rule

Randomly removes majority class samples

Removes majority sample in minimally distanced opposite-class pairs

Removes majority samples misclassified by k-nearest neighbors

Preserves Decision Boundary

Computational Complexity

O(n_maj * n_min * k)

O(n_maj * n_min * k)

O(n_maj * n_min * k)

O(n_maj)

O(n^2)

O(n * k)

Risk of Information Loss

Moderate

High

Low

High

Low

Moderate

Handles Class Overlap

Sensitive to Noise

Suitable for High-Dimensional Data

Typical Minority Class Retention

100%

100%

100%

100%

100%

100%

NEARMISS UNDER-SAMPLING

Frequently Asked Questions

Clear, technical answers to the most common questions about the NearMiss family of under-sampling algorithms for handling severe class imbalance in financial fraud detection.

NearMiss is a family of prototype selection under-sampling algorithms that reduces the majority class by selecting examples based on their average distance to the nearest minority class instances. Unlike random under-sampling, which discards data indiscriminately, NearMiss applies a heuristic to retain majority class examples that are closest to the decision boundary. The algorithm operates in three distinct variants: NearMiss-1 selects majority samples with the smallest average distance to the three closest minority samples; NearMiss-2 selects majority samples with the smallest average distance to the three farthest minority samples; and NearMiss-3 selects a fixed number of the closest majority samples for each minority sample. This distance-based selection ensures that the retained majority class examples are the most informative for defining the class boundary, making the algorithm particularly effective for imbalanced classification problems where fraudulent transactions are rare.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.