Compute what a perceptron-style neuron outputs for several inputs.
What to build
A program that evaluates a step-activation neuron on several inputs.
Requirements
- For each input print
z = Z, output O, with Z to 2 decimal places. - The output is 1 only when z is greater than 0.
Input
Line 1: the weights. Line 2: the bias. Line 3: q. Then q lines of inputs, as many as the weights.
Output
One line per input.
Constraints
1 to 20 weights; 1 ≤ q ≤ 50.
Example
Input:
1 1
-1.5
4
0 0
0 1
1 0
1 1
Output:
z = -1.50, output 0
z = -0.50, output 0
z = -0.50, output 0
z = 0.50, output 1
Why it works
The weighted sum plus bias measures which side of the neuron's boundary the input lies on, and the step turns that side into 0 or 1.
Examples
Example 1 · Input
1 1 -1.5 4 0 0 0 1 1 0 1 1
Expected output
z = -1.50, output 0 z = -0.50, output 0 z = -0.50, output 0 z = 0.50, output 1
Example 2 · Input
2 0 2 0 3
Expected output
z = 0.00, output 0 z = 6.00, output 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
z is the sum of weight × input, plus the bias.
Hint 2
The output is 1 when z > 0, otherwise 0.
Solution
Show one correct solution (try it yourself first)
weights = list(map(float, input().split()))
bias = float(input())
q = int(input())
for _ in range(q):
x = list(map(float, input().split()))
z = sum(w * v for w, v in zip(weights, x)) + bias
print(f"z = {z:.2f}, output {1 if z > 0 else 0}")