A learned rule is only as good as the answers it gets right. Score the rule "predict 1 if value ≥ cut-off" on labelled examples.
What to build
A program that applies a cut-off rule to every example and counts how many predictions are right.
Requirements
- Predict
1for a value greater than or equal to the cut-off and0otherwise. - Print the predictions on one line, separated by spaces.
- Then print
R of N correct.
Input
Line 1: the cut-off. Line 2: the values. Line 3: the labels (0 or 1), as many as the values.
Output
Two lines: the predictions, then the score.
Constraints
1 to 1000 values; values and the cut-off may be decimals.
Example
Input:
4
1 2 3 4 5 6 7 8
0 0 0 1 0 1 1 1
Output:
0 0 0 1 1 1 1 1
7 of 8 correct
Why it works
Counting the right answers is the simplest loss there is: training in the lesson picked the cut-off that made this count largest.
Examples
4 1 2 3 4 5 6 7 8 0 0 0 1 0 1 1 1
0 0 0 1 1 1 1 1 7 of 8 correct
2.5 1 3 0 1
0 1 2 of 2 correct
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Predict 1 when the value is at least the cut-off, otherwise 0.
Hint 2
A prediction is right when it equals the label at the same position.
Solution
Show one correct solution (try it yourself first)
cutoff = float(input())
values = list(map(float, input().split()))
labels = list(map(int, input().split()))
predictions = [1 if v >= cutoff else 0 for v in values]
correct = sum(1 for p, y in zip(predictions, labels) if p == y)
print(*predictions)
print(f"{correct} of {len(values)} correct")