Binary classification with logistic regression
Background
Once logistic regression is trained, prediction is a single forward pass: score each example with the linear combination , squash it through the sigmoid into a probability, and threshold at to read off a class label. Predicting class 1 when the probability is at least one-half is equivalent to predicting class 1 whenever — i.e. on the positive side of the model's linear decision boundary.
Problem statement
Implement predict_logistic(X, weights, bias) that returns the binary class predictions of a trained logistic-regression model. For each row :
Input
X—np.ndarrayof shape(N, D):Nsamples withDfeatures each.weights—np.ndarrayof shape(D,): the trained weight vector .bias—float: the trained bias .
Output
Returns an np.ndarray of shape (N,) with integer labels in — one prediction per sample.
Examples
Example 1
Input: X = [[1, 1], [2, 2], [-1, -1], [-2, -2]], weights = [1, 1], bias = 0
Output: [1, 1, 0, 0]
Explanation: the scores are . The sigmoid maps the two positive scores above (class 1) and the two negative scores below (class 0).
Constraints
- Threshold at exactly : a probability of exactly (i.e. ) predicts class 1.
- Clip into a safe range (e.g. ) before
expto avoid overflow. - Return integer labels (
0/1) as an array of shape(N,). - Vectorise with numpy — no Python loop over samples.
Notes
- Thresholding the probability at is identical to thresholding the raw score at , so you don't strictly need the sigmoid to make the decision — only if you want calibrated probabilities.
- The decision boundary is a hyperplane, which is why logistic regression is a linear classifier.
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: scores [2,4,-2,-4] -> [1,1,0,0]
- •Probability exactly 0.5 (z = 0) predicts class 1
- •Bias shifts the decision boundary
- •Returns integer labels, one per sample