Logistic Classifier Predictions

~15 mincode completion

Implement logistic_predict(X, weights, threshold) that returns an integer array of 0s and 1s.

Examples

Zero input: sigmoid=0.5, above default threshold

Input
logistic_predict([[0], [10], [-10]], [1], 0.5)
Output
[1, 1, 0]

Negative scores: all predict 0

Input
logistic_predict([[1, 2], [3, 4]], [0.5, -1], 0.5)
Output
[0, 0]

Raised threshold makes prediction stricter

Input
logistic_predict([[0], [0], [10]], [1], 0.8)
Output
[0, 0, 1]

Hints

Hint 1

applies elementwise, so negate the whole array and exponentiate it in one go.

Hint 2

Do not forget to threshold comparison. That step is easy to skip.

Requirements

  • X: Feature matrix of shape (m, n)

  • weights: Weight vector of shape (n,)

  • threshold: Probability cutoff for predicting class 1

  • Return Integer array of shape (m,) with values 0 or 1.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

7 employers weight this skill

2 health and bio companies, 2 quant funds, 2 enterprise vendors, 1 AI product company. Top match scores 85.

Python
import numpy as np

def logistic_predict(X: np.ndarray, weights: np.ndarray, threshold: float) -> np.ndarray:
    """
    Predict binary labels using logistic regression.

    Args:
        X:         Feature matrix of shape (m, n)
        weights:   Weight vector of shape (n,)
        threshold: Probability cutoff for predicting class 1

    Returns:
        Integer array of shape (m,) with values 0 or 1.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.