Tomek Links in Machine Learning

Tomek links in Machine Learning

Tomek Links: what they remove and when to try them

Tomek links are pairs of examples from different classes that are each other’s nearest neighbors. Removing selected members of these pairs is a data-cleaning approach, not a guarantee of better predictions: boundary examples can be valid observations.

In binary classification, imbalanced-learn’s default removes the majority-class member. Using sampling_strategy="all" removes both members. See the TomekLinks documentation.

Before trying it: split off evaluation data, fit scaling on training data, and apply resampling only to training rows. During cross-validation, resample inside each training fold using an imbalanced-learn pipeline. Compare against an unchanged baseline using precision and recall.

Learn the evaluation workflow first. Read the free Starter Kit excerpt on test sets to preview the €19 handbook. No email required.

Why Use Tomek Links?

  • Improve class separation
  • Reduce noise near boundaries
  • Test whether minority-class performance improves
  • Compare generalization against an unchanged baseline

They are widely used in machine learning, especially when dealing with imbalanced datasets.

How Tomek Links Work

  1. Find nearest neighbors
  2. Identify cross-class mutual neighbors
  3. Mark Tomek Links
  4. Remove majority samples

Python Implementation

from collections import Counter
from sklearn.datasets import make_classification
from imblearn.under_sampling import TomekLinks
from sklearn.model_selection import train_test_split

X, y = make_classification(
    n_samples=1000,
    weights=[0.9, 0.1],
    n_features=2,
    n_informative=2,
    n_redundant=0,
    random_state=42
)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, stratify=y, test_size=0.2, random_state=42
)
print("Before:", Counter(y_train))

tl = TomekLinks()
X_res, y_res = tl.fit_resample(X_train, y_train)

print("After:", Counter(y_res))

When to Use Tomek Links in Machine Learning

  • Imbalanced classification problems
  • Noisy datasets
  • Overlapping distributions

Comparison vs SMOTE

SMOTE creates synthetic samples, while Tomek Links remove selected cross-class nearest neighbors, which are not necessarily noise.

They can be combined, but validate the result. SMOTETomek uses a TomekLinks cleaner with sampling_strategy=’all’ by default, so it can remove members of either class.

Combine SMOTE + Tomek Links

from imblearn.combine import SMOTETomek

smt = SMOTETomek(random_state=42)
X_res, y_res = smt.fit_resample(X_train, y_train)

Best Resources to Learn Machine Learning

To go deeper into machine learning, check these curated resources:

FAQ

What is a Tomek Link?

A pair of samples from different classes that are each other’s nearest neighbors.

Do they remove minority samples?

In binary classification the default removes majority-class members; sampling_strategy=’all’ removes both members.

Are they better than random undersampling?

Not always. Tomek Links target boundary pairs; random undersampling removes examples without that condition. Compare them on the same validation data.

Final Thoughts

Tomek Links in Machine Learning are a targeted cleaning method worth testing on overlapping classes. Keep validation and test data unchanged, and retain the method only if it improves the metrics that matter for your task.

Illustration of the 90-day machine learning roadmap PDF

Stay on track. Get the Roadmap.

Download the 90-Day Roadmap PDF + get weekly tips and resources to help you succeed

Subscribe to our awesome newsletter to get the best content on your journey to learn Machine Learning, including some exclusive free goodies!

HOW IS MACHINE LEARNING