A factory camera inspects parts on a conveyor belt and labels each one "ok" or "defect". Defects are rare, so a classifier trained on these rows learns that saying "ok" is almost always safe.
When One Class Is Rare listed undersampling as one way out: throw away some rows of the common class so the rare class is no longer outvoted. It works like this:
The random choice is the only part you couldn't check, so here it has been made in advance. order is a shuffle of all the row indices, and the majority training rows you keep are the first ones you meet walking along order.
Task: write undersample(labels, split, order, ratio).
labels[i] is row i's class. The training rows contain exactly two classes, and labels can be numbers or strings.split[i] is "train" or "test".order contains every index from 0 to len(labels) - 1 exactly once.ratio is a positive whole number. With ratio = 1 you keep as many majority rows as minority rows.Return the indices of every row in the resampled dataset (the kept training rows and all the test rows) as a list in increasing order.