SVM Hyperplane Geometry Class

Quick Overview

Implement a small NumPy class for a linear SVM hyperplane that computes raw scores, signed distances, orthogonal projections onto the plane, labels and the margin width. Tests vector geometry, vectorized code, input validation and how the SVM margin relates to the weight norm.

SVM Hyperplane Geometry Class

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Complete `SVMHyperplane(w, b)` representing the hyperplane `w·x + b = 0`. Each method takes `X` of shape `(n_samples, n_features)` and returns NumPy arrays. - `decision_function(X)`: raw scores `X @ w + b`, shape `(n,)`. - `signed_distance(X)`: raw score divided by `‖w‖`. - `project(X)`: orthogonal projection of each row onto the hyperplane, shape `(n, d)`. - `predict(X)`: integer labels in `{-1, +1}` (a score of exactly `0` maps to `+1`). - `margin_width()`: float `2 / ‖w‖`. Example: ```python h = SVMHyperplane([3, 4], -5) X = [[0, 0], [1, 1], [3, 4]] h.decision_function(X) # [-5.0, 2.0, 20.0] h.signed_distance(X) # [-1.0, 0.4, 4.0] h.project([[3, 4], [0, 0]]) # [[0.6, 0.8], [0.6, 0.8]] h.margin_width() # 0.4 ``` ```hint Projection direction Ask which direction is orthogonal to the hyperplane, and how far along it a point has to move for its score to reach 0. ``` ### Constraints & Assumptions - A zero `w` (or an empty or non-finite one) raises `ValueError` in the constructor. - `X` is 2-D with `X.shape[1] == len(w)`. - Results must match to about `1e-7` relative tolerance. ### What a Strong Answer Covers - Computing `‖w‖` once in the constructor and rejecting a zero, empty or non-finite `w`. - Vectorized raw scores and the step from raw score to geometric distance. - A derivation of the projection formula and a check that projected points score 0. - The tie rule in `predict` and integer output, plus why the margin width is `2 / ‖w‖`. - Time and space cost for `n` points in `d` dimensions. ### Follow-up Questions - How would you verify that a fitted SVC's support vectors lie on the margin? - Why can't this projection be done for a kernel SVM? - How would you change `project` so points land on the margin hyperplanes `w·x + b = ±1` instead of the decision boundary?

Overview: Implement a small NumPy class for a linear SVM hyperplane that computes raw scores, signed distances, orthogonal projections onto the plane, labels and the margin width. Tests vector geometry, vectorized code, input validation and how the SVM margin relates to the weight norm.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Complete SVMHyperplane(w, b) representing the hyperplane w·x + b = 0. Each method takes X of shape (n_samples, n_features) and returns NumPy arrays.

  • decision_function(X) : raw scores X @ w + b , shape (n,) .
  • signed_distance(X) : raw score divided by ‖w‖ .
  • project(X) : orthogonal projection of each row onto the hyperplane, shape (n, d) .
  • predict(X) : integer labels in {-1, +1} (a score of exactly 0 maps to +1 ).
  • margin_width() : float 2 / ‖w‖ .

Example:

h = SVMHyperplane([3, 4], -5)
X = [[0, 0], [1, 1], [3, 4]]
h.decision_function(X)  # [-5.0, 2.0, 20.0]
h.signed_distance(X)    # [-1.0, 0.4, 4.0]
h.project([[3, 4], [0, 0]])  # [[0.6, 0.8], [0.6, 0.8]]
h.margin_width()        # 0.4

Constraints & Assumptions

  • A zero w (or an empty or non-finite one) raises ValueError in the constructor.
  • X is 2-D with X.shape[1] == len(w) .
  • Results must match to about 1e-7 relative tolerance.

What a Strong Answer Covers Guidance

  • Computing ‖w‖ once in the constructor and rejecting a zero, empty or non-finite w .
  • Vectorized raw scores and the step from raw score to geometric distance.
  • A derivation of the projection formula and a check that projected points score 0.
  • The tie rule in predict and integer output, plus why the margin width is 2 / ‖w‖ .
  • Time and space cost for n points in d dimensions.

Follow-up Questions Guidance

  • How would you verify that a fitted SVC's support vectors lie on the margin?
  • Why can't this projection be done for a kernel SVM?
  • How would you change project so points land on the margin hyperplanes w·x + b = ±1 instead of the decision boundary?