Implement a One-Class RBF SVM Decision Function and Predict

Quick Overview

Implement the decision function and label prediction for a one-class SVM with an RBF kernel, given its support vectors, dual weights and offset. It tests vectorized kernel computation in NumPy, the boundary rule for outliers, and how the formula maps onto a trained scikit-learn model.

Implement a One-Class RBF SVM Decision Function and Predict

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Complete `OneClassRBFSVM` by implementing `decision_function(X)` and `predict(X)`. The model is built from its support vectors `sv`, dual weights `dual_coef`, offset `rho` and kernel width `gamma`. The decision value for a row $x$ is $$f(x) = \sum_i \text{dual\_coef}_i \cdot \exp\left(-\gamma \lVert x - sv_i \rVert^2\right) - \rho$$ Return a 1-D float array of decision values and a 1-D int array of labels, one per row of `X`. Example: ```python m = OneClassRBFSVM([[0, 0], [1, 0]], [0.5, 0.5], rho=0.3, gamma=1.0) m.decision_function([[0, 0], [0.5, 0]]) # [0.5 + 0.5*e^-1 - 0.3, e^-0.25 - 0.3] ≈ [0.3839, 0.4788] m.predict([[0, 0], [5, 5]]) # [1, -1] ``` ```hint Vectorize the sum Compute the squared distance from every query row to every support vector in one broadcast. The sum over support vectors is then a single matrix-vector product. ``` ### Constraints & Assumptions - `predict` returns `+1` when `f(x) > 0`, otherwise `-1` (an exact `0` is an outlier). - `X` has shape `(n, d)` matching the support vectors' feature dimension `d`. - `gamma > 0`. Outputs must match scikit-learn's `OneClassSVM.decision_function` when `rho = -intercept_`. ### What a Strong Answer Covers - The decision value computed for all rows at once, with the right shapes for the kernel matrix and the output. - The labeling rule at the boundary, including a decision value of exactly 0. - How the formula lines up with a trained scikit-learn `OneClassSVM`: the sign of the intercept and the shape of the dual weights. - Time and memory cost, and where the memory goes. - Input handling for a single 1-D row and for support vectors and weights of different lengths. ### Follow-up Questions - What does `nu` control in training, and how does it relate to the outputs you computed? - How would you turn decision values into an anomaly score with a tunable contamination rate? - How would you cut memory when both the number of query rows and the number of support vectors are large?

Overview: Implement the decision function and label prediction for a one-class SVM with an RBF kernel, given its support vectors, dual weights and offset. It tests vectorized kernel computation in NumPy, the boundary rule for outliers, and how the formula maps onto a trained scikit-learn model.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Complete OneClassRBFSVM by implementing decision_function(X) and predict(X). The model is built from its support vectors sv, dual weights dual_coef, offset rho and kernel width gamma. The decision value for a row xx is

f(x)=∑idual_coefi⋅exp⁡(−γ∥x−svi∥2)−ρf(x) = \sum_i \text{dual\_coef}_i \cdot \exp\left(-\gamma \lVert x - sv_i \rVert^2\right) - \rho

Return a 1-D float array of decision values and a 1-D int array of labels, one per row of X.

Example:

m = OneClassRBFSVM([[0, 0], [1, 0]], [0.5, 0.5], rho=0.3, gamma=1.0)
m.decision_function([[0, 0], [0.5, 0]])
# [0.5 + 0.5*e^-1 - 0.3, e^-0.25 - 0.3] ≈ [0.3839, 0.4788]
m.predict([[0, 0], [5, 5]])  # [1, -1]

Constraints & Assumptions

  • predict returns +1 when f(x) > 0 , otherwise -1 (an exact 0 is an outlier).
  • X has shape (n, d) matching the support vectors' feature dimension d .
  • gamma > 0 . Outputs must match scikit-learn's OneClassSVM.decision_function when rho = -intercept_ .

What a Strong Answer Covers Guidance

  • The decision value computed for all rows at once, with the right shapes for the kernel matrix and the output.
  • The labeling rule at the boundary, including a decision value of exactly 0.
  • How the formula lines up with a trained scikit-learn OneClassSVM : the sign of the intercept and the shape of the dual weights.
  • Time and memory cost, and where the memory goes.
  • Input handling for a single 1-D row and for support vectors and weights of different lengths.

Follow-up Questions Guidance

  • What does nu control in training, and how does it relate to the outputs you computed?
  • How would you turn decision values into an anomaly score with a tunable contamination rate?
  • How would you cut memory when both the number of query rows and the number of support vectors are large?