PIXELBANKv8.2.1
Menu

Logistic Regression Prediction

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=1x)=σ(xw+b)=11+e(xw+b)P(y=1|x) = \sigma(x \cdot w + b) = \frac{1}{1 + e^{-(x \cdot w + b)}}

Then classify each sample: if P0.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:
  • We compute the dot product of each sample in XX with the weight vector ww and add the bias bb:
    • For the first sample: 10.5+20.3+0.1=0.50.6+0.1=01 \cdot 0.5 + 2 \cdot -0.3 + 0.1 = 0.5 - 0.6 + 0.1 = 0
    • For the second sample: 30.5+40.3+0.1=1.51.2+0.1=0.43 \cdot 0.5 + 4 \cdot -0.3 + 0.1 = 1.5 - 1.2 + 0.1 = 0.4
    • For the third sample: 10.5+20.3+0.1=0.5+0.6+0.1=0.2-1 \cdot 0.5 + -2 \cdot -0.3 + 0.1 = -0.5 + 0.6 + 0.1 = 0.2
  • Then, we apply the sigmoid function σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}} to each result:
    • For the first sample: σ(0)=11+e0=11+1=0.5\sigma(0) = \frac{1}{1 + e^{0}} = \frac{1}{1 + 1} = 0.5
    • For the second sample: σ(0.4)=11+e0.40.5987\sigma(0.4) = \frac{1}{1 + e^{-0.4}} \approx 0.5987
    • For the third sample: σ(0.2)=11+e0.20.5498\sigma(0.2) = \frac{1}{1 + e^{-0.2}} \approx 0.5498
  • We classify each sample based on the predicted probability, with P0.5P \geq 0.5 resulting in a prediction of 1, and P<0.5P < 0.5 resulting in a prediction of 0.
  • The final output is a list of tuples containing the predicted probabilities rounded to 4 decimal places and the corresponding predictions: (0.5,1),(0.5987,1),(0.5498,1)(0.5, 1), (0.5987, 1), (0.5498, 1)

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
Editor

Test Results

0/0
Run code to see test results.