Thinning Out the Ordinary Rows

Machine Learning in CodeEasy+10 XP+10

Thinning Out the Ordinary Rows

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:

  1. Only the training rows take part. Test rows are never dropped and never counted: they must keep looking like the real world, imbalance and all.
  2. Among the training rows, the class with more rows is the majority and the other is the minority. Every minority training row is kept.
  3. Of the majority training rows, keep only ratio×(number of minority training rows)\text{ratio} \times (\text{number of minority training rows})ratio×(number of minority training rows), chosen at random. If there aren't more than that to begin with, keep them all.

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.

Examples

labels = ["ok", "ok", "defect", "ok", "ok", "ok", "defect", "ok"]
split = ["train"] * 8
order = [5, 2, 7, 0, 3, 6, 1, 4]
ratio = 1
returns [2, 5, 6, 7]
labels = [0, 0, 1, 0, 0, 0, 0, 1, 0, 0]
split = ["train", "train", "train", "train", "test", "train", "train", "train", "test", "train"]
order = [9, 4, 1, 6, 0, 8, 3, 7, 2, 5]
ratio = 2
returns [0, 1, 2, 4, 6, 7, 8, 9]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run