Loading problem...
In the field of computational classification, Support Vector Machines (SVMs) stand as one of the most elegant and powerful algorithms for binary classification. At the heart of a linear SVM lies a fundamental geometric concept: the margin width, which quantifies the separation gap between two classes.
An SVM seeks to find an optimal decision hyperplane that separates two classes with maximum clearance. This hyperplane is defined by a weight vector w and a bias term b, expressed as the equation:
$$\mathbf{w} \cdot \mathbf{x} + b = 0$$
The margin is the region bounded by two parallel hyperplanes that pass through the support vectors (the data points closest to the decision boundary) of each class. These bounding hyperplanes are:
The margin width represents the total perpendicular distance between these two bounding hyperplanes. Mathematically, this width is computed as:
$$\text{Margin Width} = \frac{2}{|\mathbf{w}|}$$
where (|\mathbf{w}|) is the Euclidean (L2) norm of the weight vector:
$$|\mathbf{w}| = \sqrt{\sum_{i=1}^{d} w_i^2}$$
A larger margin indicates better class separation and typically leads to improved generalization on unseen data. The SVM optimization problem aims to maximize this margin while correctly classifying training samples.
Your Task: Write a Python function that computes the margin width given the weight vector w of a trained linear SVM classifier. Your function should:
w = np.array([3.0, 4.0])0.4The weight vector w = [3.0, 4.0] defines a 2D decision hyperplane.
Step 1: Calculate the L2 norm (Euclidean length): ||w|| = √(3² + 4²) = √(9 + 16) = √25 = 5
Step 2: Apply the margin width formula: Margin Width = 2 / ||w|| = 2 / 5 = 0.4
This means the total perpendicular separation between the two margin boundaries is 0.4 units.
w = np.array([1.0])2.0This is a 1-dimensional classification scenario (separating points on a number line).
Step 1: Calculate the L2 norm: ||w|| = √(1²) = 1
Step 2: Apply the margin width formula: Margin Width = 2 / ||w|| = 2 / 1 = 2.0
With a unit weight, the margin spans 2 units in the feature space.
w = np.array([1.0, 2.0, 2.0])0.6666666667The weight vector w = [1.0, 2.0, 2.0] defines a hyperplane in 3D space.
Step 1: Calculate the L2 norm: ||w|| = √(1² + 2² + 2²) = √(1 + 4 + 4) = √9 = 3
Step 2: Apply the margin width formula: Margin Width = 2 / ||w|| = 2 / 3 ≈ 0.6666666667
The three-dimensional separating hyperplane has a margin width of approximately 0.667 units.
Constraints