Don't Lose the Rare Ones

Machine Learning in CodeMedium+10 XP+10

Don't Lose the Rare Ones

A bank has a list of transactions. Almost all are "ok", and only a few are "fraud". A plain random split can leave the test set with almost no fraud at all.

Task: write rare_in_test(labels, rare_label, test_size, seed).

  • labels is a list of labels, one per transaction. rare_label is the label you care about.
  • Do a plain split of labels with train_test_split(labels, test_size=test_size, random_state=seed).
  • Do a stratified split with the same arguments plus stratify=labels.
  • Return [plain, stratified]: how many times rare_label appears in the test part of each split.

Examples

labels = ["ok"] * 36 + ["fraud"] * 4
rare_label = "fraud"
test_size = 0.25
seed = 0
returns [0, 1]
labels = ["ok"] * 90 + ["fraud"] * 10
rare_label = "fraud"
test_size = 0.2
seed = 1
returns [2, 2]

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