Logistic Regression Decision Boundary as a Slope and Intercept

Quick Overview

Convert the decision boundary of a two-feature logistic regression into slope-intercept form and handle the vertical-line case without dividing by zero. It tests the algebra behind linear decision boundaries, degenerate weight vectors, and returning plain Python floats.

Logistic Regression Decision Boundary as a Slope and Intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given a 1-D `float64` array `w` of shape `(2,)` and a Python float `b`, implement `boundary_slope_intercept(w, b)` to rewrite the decision boundary `w[0]*x1 + w[1]*x2 + b = 0` of a two-feature logistic regression as `x2 = slope * x1 + intercept`. Return the tuple `(slope, intercept)`. ```python def boundary_slope_intercept(w: np.ndarray, b: float) -> tuple[float, float]: ... ``` Requirements: - Return a tuple of two Python floats, not an `ndarray`. - When `w[1] != 0.0`, return `(-w[0]/w[1], -b/w[1])`. - When `w[1] == 0.0` (the vertical line `x1 = -b/w[0]`), return `(float('inf'), float('nan'))`. - Do not divide by zero or raise in the degenerate case. For example: ```python boundary_slope_intercept(np.array([1.0, 2.0]), -4.0) # (-0.5, 2.0) boundary_slope_intercept(np.array([3.0, 4.0]), -10.0) # (-0.75, 2.5) boundary_slope_intercept(np.array([2.0, 0.0]), -6.0) # (inf, nan) ``` ```hint Order the checks Find the one step of solving for `x2` that can fail, and test for that case before any division. Then check the types you return. ``` ### What a Strong Answer Covers - The algebra that turns `w[0]*x1 + w[1]*x2 + b = 0` into slope-intercept form, and what the boundary means for the model's probabilities. - A guard for `w[1] == 0.0` that runs before any division and returns the required sentinels. - Conversion of NumPy scalars to Python floats in the returned tuple. - Degenerate inputs such as `w = [0, 0]` and negative zero. ### Follow-up Questions - How would you extend this to plot a boundary segment over a given `x1` range? - Why check `w[1] == 0.0` exactly instead of using a small tolerance, and when would a tolerance be better? - How does the line change if the classifier predicts positive at a probability threshold other than 0.5?

Overview: Convert the decision boundary of a two-feature logistic regression into slope-intercept form and handle the vertical-line case without dividing by zero. It tests the algebra behind linear decision boundaries, degenerate weight vectors, and returning plain Python floats.

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

Given a 1-D float64 array w of shape (2,) and a Python float b, implement boundary_slope_intercept(w, b) to rewrite the decision boundary w[0]*x1 + w[1]*x2 + b = 0 of a two-feature logistic regression as x2 = slope * x1 + intercept. Return the tuple (slope, intercept).

def boundary_slope_intercept(w: np.ndarray, b: float) -> tuple[float, float]:
    ...

Requirements:

  • Return a tuple of two Python floats, not an ndarray .
  • When w[1] != 0.0 , return (-w[0]/w[1], -b/w[1]) .
  • When w[1] == 0.0 (the vertical line x1 = -b/w[0] ), return (float('inf'), float('nan')) .
  • Do not divide by zero or raise in the degenerate case.

For example:

boundary_slope_intercept(np.array([1.0, 2.0]), -4.0)   # (-0.5, 2.0)
boundary_slope_intercept(np.array([3.0, 4.0]), -10.0)  # (-0.75, 2.5)
boundary_slope_intercept(np.array([2.0, 0.0]), -6.0)   # (inf, nan)

What a Strong Answer Covers Guidance

  • The algebra that turns w[0]*x1 + w[1]*x2 + b = 0 into slope-intercept form, and what the boundary means for the model's probabilities.
  • A guard for w[1] == 0.0 that runs before any division and returns the required sentinels.
  • Conversion of NumPy scalars to Python floats in the returned tuple.
  • Degenerate inputs such as w = [0, 0] and negative zero.

Follow-up Questions Guidance

  • How would you extend this to plot a boundary segment over a given x1 range?
  • Why check w[1] == 0.0 exactly instead of using a small tolerance, and when would a tolerance be better?
  • How does the line change if the classifier predicts positive at a probability threshold other than 0.5?