#178Perceptron — the hard threshold unitEasy

Perceptron — the hard threshold unit

Background

Rosenblatt's perceptron (1958) is a binary linear classifier with a hard decision: it fires when the score is non-negative.

y^=1[z0],z=wx+b\hat{y} = \mathbb{1}[\,z \ge 0\,], \qquad z = \mathbf{w}^\top\mathbf{x} + b

Unlike the smooth sigmoid, this step activation jumps from 0 to 1 at z=0z = 0. Same linear boundary as logistic regression, but a discontinuous output.

Problem statement

Implement perceptron(x, w, b) that returns the perceptron's hard label: 1 when wx+b0\mathbf{w}^\top\mathbf{x}+b \ge 0, else 0.

Input

  • x — array-like feature vector.
  • w — array-like weight vector, same length.
  • bfloat, the bias.

Output

Returns an int, 0 or 1.

Examples

Example 1

Input:  x = [2, 3], w = [2, 3], b = -5
Output: 1

Explanation: z=80z = 8 \ge 0, so the unit fires.

Example 2 — on the threshold fires

Input:  x = [2.5, 0], w = [2, 3], b = -5
Output: 1

Explanation: z=0z = 0, and the rule is 0\ge 0, so it still outputs 1.

Constraints

  • Use 0\ge 0 (inclusive) for the threshold, matching 1[z0]\mathbb{1}[z \ge 0].
  • Return a Python int (0/1).

Notes

  • The step has zero gradient almost everywhere, which is exactly why modern neurons swap it for a differentiable activation so backpropagation can flow.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example — positive score fires
  • Reference on the threshold fires
  • Sample negative score stays zero