Logistic regression turns a score into a probability with the sigmoid. Convert some scores.
What to build
A program that prints the sigmoid of each score and the class it predicts.
Requirements
- For each score print
Z -> P (C), with Z written as in the input, P with 4 decimal places and C the class, 1 or 0. - The class is 1 when the probability is at least 0.5.
Input
One line of scores.
Output
One line per score.
Constraints
1 to 100 scores between −100 and 100.
Example
Input:
0 2 -2
Output:
0 -> 0.5000 (1)
2 -> 0.8808 (1)
-2 -> 0.1192 (0)
Why it works
The sigmoid is 0.5 at z = 0 and rises smoothly towards 1 for positive scores and falls towards 0 for negative ones, so the sign of the score decides the class.
Examples
Example 1 · Input
0 2 -2
Expected output
0 -> 0.5000 (1) 2 -> 0.8808 (1) -2 -> 0.1192 (0)
Example 2 · Input
5
Expected output
5 -> 0.9933 (1)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
σ(z) = 1 / (1 + e^(−z)); use `math.exp`.
Hint 2
The class is 1 when σ(z) ≥ 0.5, which is the same as z ≥ 0.
Solution
Show one correct solution (try it yourself first)
import math
for token in input().split():
p = 1 / (1 + math.exp(-float(token)))
print(f"{token} -> {p:.4f} ({1 if p >= 0.5 else 0})")