v9.1.1
Menu

RBF Kernel Matrix

hardFREESupport Vector MachinesMachine Learning 1

Compute the Radial Basis Function (RBF/Gaussian) kernel matrix.

The RBF kernel is: K(x,y)=exp⁡(−∥x−y∥22σ2)K(x, y) = \exp\left(-\frac{\|x - y\|^2}{2\sigma^2}\right)

Given a set of vectors XX (n points) and parameter σ\sigma, compute the n×nn \times n kernel matrix.

Return the matrix with values rounded to 4 decimal places. The diagonal should be all 1.0 (each point compared to itself).

Example

Input
X = [[0, 0], [1, 0], [0, 1]]
sigma = 1.0
Output
[[1.0, 0.6065, 0.6065], [0.6065, 1.0, 0.3679], [0.6065, 0.3679, 1.0]]
Reasoning
  • Inputs: X holds n=3n = 3 points in the plane, p0=(0,0)p_0 = (0, 0), p1=(1,0)p_1 = (1, 0), p2=(0,1)p_2 = (0, 1), and sigma =1.0= 1.0 sets how quickly similarity fades with distance. With σ=1\sigma = 1 the denominator is 2σ2=22\sigma^2 = 2.
  • Squared distances: ∥p0−p1∥2=12+02=1\|p_0 - p_1\|^2 = 1^2 + 0^2 = 1, ∥p0−p2∥2=02+12=1\|p_0 - p_2\|^2 = 0^2 + 1^2 = 1, and ∥p1−p2∥2=12+(−1)2=2\|p_1 - p_2\|^2 = 1^2 + (-1)^2 = 2.
  • Kernel values: the two unit-distance pairs give e−1/2≈0.6065e^{-1/2} \approx 0.6065; the pair at squared distance 2 gives e−2/2=e−1≈0.3679e^{-2/2} = e^{-1} \approx 0.3679. Each point against itself has distance 0, so e0=1.0e^{0} = 1.0 on the diagonal.
  • Output: a 3×33 \times 3 matrix where entry (row ii, column jj) is K(pi,pj)K(p_i, p_j), rounded to 4 decimal places: [[1.0, 0.6065, 0.6065], [0.6065, 1.0, 0.3679], [0.6065, 0.3679, 1.0]].
  • Notice: the matrix is symmetric because ∥pi−pj∥=∥pj−pi∥\|p_i - p_j\| = \|p_j - p_i\|, and every value lies in (0,1](0, 1]. A smaller σ\sigma shrinks the off-diagonal entries towards 0; a larger one pushes them towards 1.

Constraints

  • X: 2D list (n x features)
  • sigma: positive float
  • Return n x n symmetric kernel matrix
  • Round each value to 4 decimal places

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.