Signed Distance to Decision Boundary

Quick Overview

Implement a vectorized NumPy function that returns each sample's signed Euclidean distance to a logistic regression decision boundary. It tests the point-to-hyperplane formula, why the raw score is normalized by the weight norm, and how the sign maps to the predicted class.

Signed Distance to Decision Boundary

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `signed_distance_to_boundary(X, w, b)` given a float64 matrix `X` of shape `(n_samples, n_features)`, a not-all-zero float64 vector `w` of shape `(n_features,)`, and a float intercept `b`. Return a float64 array of shape `(n_samples,)` where element `i` is `(X[i] @ w + b) / ||w||_2` (Euclidean norm). Here `w` and `b` are the coefficients of a logistic regression model, so the predicted probability for a sample is `p = sigmoid(X[i] @ w + b)`. The function must meet two requirements: - Keep the sign: positive means the predicted-positive side (`p > 0.5`), negative means the predicted-negative side. - Return exactly `0.0` for samples on the boundary (`p = 0.5`). ```python w = np.array([3.0, 4.0]) # ||w||_2 == 5.0 b = -10.0 X = np.array([[2.0, 1.0], [0.0, 0.0], [10.0, 10.0], [2.0, 0.5]]) signed_distance_to_boundary(X, w, b) # -> [0.0, -2.0, 12.0, -0.4] # X[0] gives z = 0 exactly, so its distance is 0.0 ``` ```hint One pass over the data A single matrix-vector product gives every raw score at once. Then think about what dividing by a positive number does to a score's sign. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n_samples, n_features)`, and `w` is float64 with shape `(n_features,)`. - `w` is not all zero, so `||w||_2 > 0`, and `b` is a Python float. ### What a Strong Answer Covers - Computes all raw scores with one vectorized matrix-vector product and the norm of `w` once. - Explains why dividing by the Euclidean norm turns the raw score into a geometric distance. - Shows why the sign and the exact zero on the boundary survive the division, and when floating point can break the exact zero. - States the time and space cost and how to handle an all-zero `w`. ### Follow-up Questions - Why divide by `||w||_2` instead of returning the raw score `X @ w + b`? - How would you handle the case `w = 0`? - If you multiply both `w` and `b` by 2, what happens to the predicted probabilities, and what happens to the distances?

Overview: Implement a vectorized NumPy function that returns each sample's signed Euclidean distance to a logistic regression decision boundary. It tests the point-to-hyperplane formula, why the raw score is normalized by the weight norm, and how the sign maps to the predicted class.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement signed_distance_to_boundary(X, w, b) given a float64 matrix X of shape (n_samples, n_features), a not-all-zero float64 vector w of shape (n_features,), and a float intercept b. Return a float64 array of shape (n_samples,) where element i is (X[i] @ w + b) / ||w||_2 (Euclidean norm).

Here w and b are the coefficients of a logistic regression model, so the predicted probability for a sample is p = sigmoid(X[i] @ w + b). The function must meet two requirements:

  • Keep the sign: positive means the predicted-positive side ( p > 0.5 ), negative means the predicted-negative side.
  • Return exactly 0.0 for samples on the boundary ( p = 0.5 ).
w = np.array([3.0, 4.0])   # ||w||_2 == 5.0
b = -10.0
X = np.array([[2.0, 1.0], [0.0, 0.0], [10.0, 10.0], [2.0, 0.5]])

signed_distance_to_boundary(X, w, b)
# -> [0.0, -2.0, 12.0, -0.4]
# X[0] gives z = 0 exactly, so its distance is 0.0

Constraints & Assumptions

  • X is float64 with shape (n_samples, n_features) , and w is float64 with shape (n_features,) .
  • w is not all zero, so ||w||_2 > 0 , and b is a Python float.

What a Strong Answer Covers Guidance

  • Computes all raw scores with one vectorized matrix-vector product and the norm of w once.
  • Explains why dividing by the Euclidean norm turns the raw score into a geometric distance.
  • Shows why the sign and the exact zero on the boundary survive the division, and when floating point can break the exact zero.
  • States the time and space cost and how to handle an all-zero w .

Follow-up Questions Guidance

  • Why divide by ||w||_2 instead of returning the raw score X @ w + b ?
  • How would you handle the case w = 0 ?
  • If you multiply both w and b by 2, what happens to the predicted probabilities, and what happens to the distances?