BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Data processing and features46 of 54 in topic

819. Create synthetic minority samples like SMOTE

MediumData processing and features Pro

Why it matters Fraud, defect and rare-intent classifiers often train on SMOTE-balanced data.

SMOTE balances a dataset by inventing minority-class points on the line between a real minority point and one of its nearest minority neighbours.

Write smote_points(X, k, picks). X is (n, d) minority samples. For each (i, nb, lam) in picks: find point i's k nearest neighbours among the OTHER rows of X by Euclidean distance (ties: lower index), take the nb-th of them (0-based, nearest first) as z, and create X[i] + lam * (z - X[i]). Return an array of shape (len(picks), d).

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef smote_points(X, k, picks)

Example 1

Input: smote_points([[0.0, 0.0], [1.0, 0.0], [0.0, 2.0], [5.0, 5.0]], 2, [(0, 0, 0.5)])

Output: [[0.5, 0.0]]

Example 2

Input: X = [[0.0, 0.0], [1.0, 0.0], [0.0, 2.0], [5.0, 5.0]], k = 2, picks = [(0, 1, 0.25), (3, 0, 1.0)]

Output: [[0.0, 0.5], [0.0, 2.0]]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Compute distances from X[i] to every row and sort by (distance, index).

Hint 2

Skip row i itself before taking the first k.

Hint 3

Interpolate with lam between X[i] and the chosen neighbour.

Common mistakes
  • Including the point itself as its own nearest neighbour.
  • Interpolating toward majority-class points.
Understand the idea

Each synthetic point lies on the segment between a real minority point and a nearby minority point, so it stays inside the minority region instead of copying points exactly.

Worked example

point (0, 0) has nearest neighbour (1, 0); lam 0.5 gives (0.5, 0).

new = x + lam (z - x), z one of x's k nearest minority neighbours
Topics
Data processing and features
Similar questions
  • Split a dataset so every class keeps its shareMedium
  • Bin a feature into quantile buckets learned from training dataMedium
  • Frequency-encode a category with a shared bucket for rare and unseen valuesMedium
  • Generate K-fold train and validation index splitsMedium
All Data processing and features problems
Report
819 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def smote_points(X, k, picks):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS