SVM Geometric Margin

~10 mincode completion

Implement svm_margin(w) that returns the geometric margin given the weight vector.

Examples

Unit vector: margin = 2

Input
svm_margin([1, 0])
Output
2

||w||=2: margin = 1

Input
svm_margin([2, 0])
Output
1

3-4-5 right triangle: ||w||=5, margin=0.4

Input
svm_margin([3, 4])
Output
0.4

Hints

Hint 1

gives the magnitude in one call; pick the axis deliberately.

Hint 2

Return 2 rather than 1 over norm.

Requirements

  • : Weight vector (normal to the decision boundary)

  • Return Margin = 2 / ||w||.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

7 employers weight this skill

2 health and bio companies, 2 quant funds, 2 enterprise vendors, 1 AI product company. Top match scores 87.

Python
import numpy as np

def svm_margin(w: np.ndarray) -> float:
    """
    Compute the geometric margin of an SVM with weight vector w.

    Args:
        w: Weight vector (normal to the decision boundary)

    Returns:
        Margin = 2 / ||w||.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.