The first score anyone computes for a classifier: how often it is right.
What to build
A program that compares actual and predicted labels and reports the accuracy and the number of errors.
Requirements
- Print
accuracy Awith 3 decimal places. - Print
errors E, the number of mismatches.
Input
Line 1: the actual labels. Line 2: the predicted labels (same count). Labels are words.
Output
Two lines.
Constraints
1 to 1000 labels.
Example
Input:
cat dog cat
cat cat cat
Output:
accuracy 0.667
errors 1
Why it works
Accuracy is the share of predictions that match; it works for any number of classes, but it treats every mistake as equally costly.
Examples
Example 1 · Input
cat dog cat cat cat cat
Expected output
accuracy 0.667 errors 1
Example 2 · Input
a a
Expected output
accuracy 1.000 errors 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Pair the actual and predicted labels with `zip`.
Hint 2
Accuracy is the number of matches divided by the number of examples.
Solution
Show one correct solution (try it yourself first)
actual = input().split()
predicted = input().split()
right = sum(1 for a, p in zip(actual, predicted) if a == p)
print(f"accuracy {right / len(actual):.3f}")
print(f"errors {len(actual) - right}")