Log loss and accuracy

Score a probabilistic classifier two ways: the log loss, which looks at the probabilities, and the accuracy, which only looks at the final answers.

What to build

A program that computes the average log loss and the accuracy of some predicted probabilities.

Requirements

  • Log loss: the mean over examples of −log(q), where q is the probability given to the true class, replaced by 10⁻¹⁵ if it is smaller.
  • Accuracy: predict 1 when p ≥ 0.5, else 0, and count matches.
  • Print log loss L with 4 decimal places and accuracy A with 3.

Input

Line 1: the labels (0 or 1). Line 2: the predicted probabilities of class 1.

Output

Two lines.

Constraints

1 to 1000 examples; 0 ≤ p ≤ 1.

Example

Input:

1 0
0.9 0.2

Output:

log loss 0.1643
accuracy 1.000

Why it works

The log loss rewards confidence in the right answer and punishes confidence in the wrong one, which accuracy cannot see: 0.51 and 0.99 count the same for accuracy.

Examples

Example 1 · Input
1 0
0.9 0.2
Expected output
log loss 0.1643
accuracy 1.000
Example 2 · Input
1
0.5
Expected output
log loss 0.6931
accuracy 1.000

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

Hints

Hint 1

For each example, the probability of the true class is p if y = 1 and 1 − p if y = 0.

Hint 2

The example's loss is −log of that probability, after raising it to at least 10⁻¹⁵.

Hint 3

Predict 1 when p ≥ 0.5 for the accuracy.

Solution

Show one correct solution (try it yourself first)
import math

labels = list(map(int, input().split()))
probs = list(map(float, input().split()))
total = 0.0
right = 0
for y, p in zip(labels, probs):
    q = p if y == 1 else 1 - p
    total += -math.log(max(q, 1e-15))
    right += (1 if p >= 0.5 else 0) == y
print(f"log loss {total / len(labels):.4f}")
print(f"accuracy {right / len(labels):.3f}")

Lesson: Logistic regression →