v9.1.1
Menu

Logistic Regression Prediction

mediumFREEClassificationMachine Learning 1

Implement the prediction step of logistic regression.

Given a feature matrix XX (without bias), a weight vector ww, and a bias bb, compute the probability for each sample using:

P(y=1∣x)=σ(x⋅w+b)=11+e−(x⋅w+b)P(y=1|x) = \sigma(x \cdot w + b) = \frac{1}{1 + e^{-(x \cdot w + b)}}

Then classify each sample: if P≥0.5P \geq 0.5, predict 1; otherwise predict 0.

Return a list of tuples [(probability, prediction), ...] with probabilities rounded to 4 decimal places.

Example

Input
X = [[1, 2], [3, 4], [-1, -2]]
w = [0.5, -0.3]
b = 0.1
Output
[(0.5, 1), (0.5987, 1), (0.5498, 1)]
Reasoning
  • Inputs: X holds three samples of two features; w weights each feature and b = 0.1 shifts every score equally.
  • Linear score z=x⋅w+bz = x \cdot w + b for each row:
    • (1)(0.5)+(2)(−0.3)+0.1=0.5−0.6+0.1=0(1)(0.5) + (2)(-0.3) + 0.1 = 0.5 - 0.6 + 0.1 = 0
    • (3)(0.5)+(4)(−0.3)+0.1=1.5−1.2+0.1=0.4(3)(0.5) + (4)(-0.3) + 0.1 = 1.5 - 1.2 + 0.1 = 0.4
    • (−1)(0.5)+(−2)(−0.3)+0.1=−0.5+0.6+0.1=0.2(-1)(0.5) + (-2)(-0.3) + 0.1 = -0.5 + 0.6 + 0.1 = 0.2
  • Squash with the sigmoid: σ(0)=11+1=0.5\sigma(0) = \frac{1}{1+1} = 0.5, σ(0.4)=11+0.6703≈0.5987\sigma(0.4) = \frac{1}{1 + 0.6703} \approx 0.5987, σ(0.2)=11+0.8187≈0.5498\sigma(0.2) = \frac{1}{1 + 0.8187} \approx 0.5498, each rounded to 4 decimal places.
  • Threshold: all three probabilities are ≥0.5\geq 0.5, so every label is 1, giving [(0.5, 1), (0.5987, 1), (0.5498, 1)].
  • Notice the first sample lands exactly on the decision boundary (z=0z = 0, P=0.5P = 0.5) and still counts as class 1 because the rule is ≥\geq. The third row negates the first, yet scores 0.20.2, not 00: the bias does not flip sign with xx.

Constraints

  • X is a 2D list (n_samples x n_features)
  • w is a list of n_features weights
  • b is a scalar bias
  • Return list of (probability, prediction) tuples
  • Probabilities rounded to 4 decimal places

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.