Recover the x2 coordinate of a two-feature logistic regression decision boundary

Quick Overview

Write a vectorized NumPy function that returns the x2 coordinate on the 0.5-probability decision boundary of a two-feature logistic regression for each x1 value. It tests how the score relates to the sigmoid, solving the boundary equation, and floating-point behavior.

Recover the x2 coordinate of a two-feature logistic regression decision boundary

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `decision_boundary_x2(w, b, x1)` for a 2-feature logistic regression with score `z = w[0]*x1 + w[1]*x2 + b`. Given float64 `w` of shape `(2,)`, Python float `b`, and float64 `x1` of shape `(m,)`, return the `x2` on the decision boundary (`z == 0`, probability 0.5) for each element. Requirements: - Compute `x2 = -(b + w[0]*x1) / w[1]` elementwise. - Return a 1-D float64 `np.ndarray` of shape `(m,)`. - Substituting each output back gives `sigmoid(w[0]*x1 + w[1]*out + b)` equal to 0.5, up to floating-point rounding. For example: ```python w = np.array([1.0, 2.0]); b = -4.0 x1 = np.array([0.0, 1.0, 2.0, 4.0]) decision_boundary_x2(w, b, x1) # -> array([2.0, 1.5, 1.0, 0.0]) ``` ```hint Work in score space Write the boundary as a condition on z, solve the linear equation for x2, and keep the computation vectorized over x1. ``` ### Constraints & Assumptions - `w[1] != 0` is guaranteed, so no divide-by-zero handling is required. - `x1` can hold any number of elements `m`, including large values. ### What a Strong Answer Covers - Why the 0.5 probability boundary is exactly the set of points where the score is 0 - Solving for `x2` correctly, including the sign, and vectorizing over `x1` - Returning a float64 array of shape `(m,)` - How floating-point rounding and a small `w[1]` affect the result ### Follow-up Questions - What happens to the boundary when `w[1]` approaches 0? - How would you draw a decision region instead of a line? - How does the boundary move if you predict the positive class only when the probability exceeds 0.7?

Overview: Write a vectorized NumPy function that returns the x2 coordinate on the 0.5-probability decision boundary of a two-feature logistic regression for each x1 value. It tests how the score relates to the sigmoid, solving the boundary equation, and floating-point behavior.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement decision_boundary_x2(w, b, x1) for a 2-feature logistic regression with score z = w[0]*x1 + w[1]*x2 + b. Given float64 w of shape (2,), Python float b, and float64 x1 of shape (m,), return the x2 on the decision boundary (z == 0, probability 0.5) for each element.

Requirements:

  • Compute x2 = -(b + w[0]*x1) / w[1] elementwise.
  • Return a 1-D float64 np.ndarray of shape (m,) .
  • Substituting each output back gives sigmoid(w[0]*x1 + w[1]*out + b) equal to 0.5, up to floating-point rounding.

For example:

w = np.array([1.0, 2.0]); b = -4.0
x1 = np.array([0.0, 1.0, 2.0, 4.0])
decision_boundary_x2(w, b, x1)
# -> array([2.0, 1.5, 1.0, 0.0])

Constraints & Assumptions

  • w[1] != 0 is guaranteed, so no divide-by-zero handling is required.
  • x1 can hold any number of elements m , including large values.

What a Strong Answer Covers Guidance

  • Why the 0.5 probability boundary is exactly the set of points where the score is 0
  • Solving for x2 correctly, including the sign, and vectorizing over x1
  • Returning a float64 array of shape (m,)
  • How floating-point rounding and a small w[1] affect the result

Follow-up Questions Guidance

  • What happens to the boundary when w[1] approaches 0?
  • How would you draw a decision region instead of a line?
  • How does the boundary move if you predict the positive class only when the probability exceeds 0.7?