A bootstrap sample and its out-of-bag examples

Bagging trains each model on a bootstrap sample. Given the random draws, build the sample and find what it left out.

What to build

A program that builds a bootstrap sample from given draws and lists the out-of-bag values.

Requirements

  • Print sample: followed by the drawn values, written as in the input.
  • Print out-of-bag: followed by the values never drawn, or out-of-bag: none.
  • Print sample mean M with 2 decimal places.

Input

Line 1: the values. Line 2: the draws, positions counted from 0, as many as there are values.

Output

Three lines.

Constraints

1 to 1000 values; every draw is a valid position.

Example

Input:

10 20 30 40 50
0 0 3 4 1

Output:

sample: 10 10 40 50 20
out-of-bag: 30
sample mean 26.00

Why it works

Drawing with replacement repeats some examples and skips others, so each bootstrap sample is a slightly different dataset, and the skipped examples can test the model trained on it.

Examples

Example 1 · Input
10 20 30 40 50
0 0 3 4 1
Expected output
sample: 10 10 40 50 20
out-of-bag: 30
sample mean 26.00
Example 2 · Input
5 7
1 0
Expected output
sample: 7 5
out-of-bag: none
sample mean 6.00

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

The sample is the values at the drawn positions, in the order drawn.

Hint 2

Out-of-bag values are those whose position was never drawn, in their original order.

Solution

Show one correct solution (try it yourself first)
values = input().split()
draws = list(map(int, input().split()))
sample = [values[i] for i in draws]
drawn = set(draws)
out = [v for i, v in enumerate(values) if i not in drawn]
print("sample:", *sample)
print("out-of-bag:", *out) if out else print("out-of-bag: none")
print(f"sample mean {sum(float(v) for v in sample) / len(sample):.2f}")

Lesson: Ensembles: random forests and boosting →