#311Logistic regression — probability from a scoreEasy

Logistic regression — probability from a score

Background

Logistic regression turns a linear score (the logit) into a probability with the sigmoid:

z=wx+b,P(y=1x)=σ(z)=11+ezz = \mathbf{w}^\top \mathbf{x} + b, \qquad \mathbb{P}(y=1 \mid \mathbf{x}) = \sigma(z) = \frac{1}{1 + e^{-z}}

The linear part is the same wx+b\mathbf{w}^\top\mathbf{x}+b you met in linear regression; the sigmoid is the only new piece, and it guarantees the output lands in (0,1)(0, 1) so it can be read as a probability.

Problem statement

Implement predict_proba(x, w, b) that returns σ(wx+b)\sigma(\mathbf{w}^\top\mathbf{x} + b) for a single sample.

Input

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

Output

Returns a Python float in (0,1)(0, 1): the predicted probability of class 1.

Examples

Example 1 — a zero score is a coin flip

Input:  x = [0, 0], w = [1, 1], b = 0
Output: 0.5

Explanation: z=0z = 0, and σ(0)=0.5\sigma(0) = 0.5 — exactly the toss-up point.

Example 2 — a positive score leans toward class 1

Input:  x = [1, 1], w = [1, 2], b = -1
Output: 0.8807970779...

Explanation: z=11+211=2z = 1\cdot1 + 2\cdot1 - 1 = 2, and σ(2)0.8808\sigma(2) \approx 0.8808.

Constraints

  • Compute the dot product wx\mathbf{w}^\top\mathbf{x}, add b, then apply the sigmoid.
  • Return a plain float.

Notes

  • This is the entire logistic-regression forward pass for one example. Stacking many samples just turns the dot product into a matrix multiply Xw+bX\mathbf{w} + b.
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 — a zero score is a coin flip
  • Reference — score z = 2 -> sigmoid(2)
  • Sample — score z = -3 -> sigmoid(-3)