Classify rows using given weights w and bias b (no training). For each row: z = dot(w,x)+b; p = sigmoid(z); class = 1 if p>=0.5 else 0. Library: NumPy matrix multiply X@w+b, then scipy.special.expit and threshold. RESULT: list of predicted classes (0/1).

By hand

With NumPy

X @ w + b computes all dot products in one vectorized step. expit applies sigmoid element-wise; list comprehension thresholds at 0.5.

naive.py
import math
w = [1.0, -1.0]
b = 0.0
X = [[2, 1], [1, 3], [4, 2]]
classes = []
for row in X:
    z = w[0] * row[0] + w[1] * row[1] + b
    p = 1 / (1 + math.exp(-z))
    c = 1 if p >= 0.5 else 0
    classes.append(c)
print('RESULT:', classes)
library.py
import numpy as np
from scipy.special import expit
from dalib.display import set_display
set_display()

w = np.array([1.0, -1.0])
b = 0.0
X = np.array([[2, 1], [1, 3], [4, 2]])
z = X @ w + b
raw_probs = expit(z)
probs = [round(float(p), 4) for p in raw_probs]
classes = [1 if float(p) >= 0.5 else 0 for p in raw_probs]
print('z scores:', [round(float(v), 4) for v in z])
print('probs:', probs)
print('RESULT:', classes)
z scores: [1.0, -2.0, 2.0]
probs: [0.7311, 0.1192, 0.8808]
RESULT: [1, 0, 1]

Implementation notes

  • Weights are given, not trained — this lesson isolates the predict step. Training (e.g. gradient descent on log-loss) is a separate concern.
  • The linear score z = dot(w,x)+b is the same as in linear regression; sigmoid converts it to a probability. Cross-reference: normal-equation-1d (ch03) for the linear-score part.
  • Threshold 0.5 corresponds to z=0 (s(0)=0.5). Different thresholds trade precision against recall — not shown here.
  • X @ w requires X as a 2D numpy array and w as a 1D array; the result is a 1D array of scores, one per row.
  • Cross-reference: sigmoid-function (this chapter) for the activation; sklearn's LogisticRegression.predict_proba wraps the same computation after fitting.