PIXELBANKv8.2.1
Menu

RBF Kernel Matrix

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

The RBF kernel is: K(x,y)=exp(xy22σ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:
  • The RBF kernel matrix is computed by calculating K(x,y)=exp(xy22σ2)K(x, y) = \exp\left(-\frac{\|x - y\|^2}{2\sigma^2}\right) for each pair of points xx and yy in the input set XX.
  • For the given input X=[[0,0],[1,0],[0,1]]X = [[0, 0], [1, 0], [0, 1]] and σ=1.0\sigma = 1.0, we calculate the pairwise distances and apply the RBF kernel formula to obtain the kernel matrix values.
  • The diagonal elements are all 1.01.0 since xx2=0\|x - x\|^2 = 0, resulting in K(x,x)=exp(0)=1.0K(x, x) = \exp(0) = 1.0 for each point xx.
  • The off-diagonal elements are calculated using the RBF kernel formula, e.g., K([0,0],[1,0])=exp((01)2+(00)221.02)=exp(12)0.6065K([0, 0], [1, 0]) = \exp\left(-\frac{\|(0-1)^2 + (0-0)^2\|}{2 \cdot 1.0^2}\right) = \exp\left(-\frac{1}{2}\right) \approx 0.6065.

Constraints:

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

Test Results

0/0
Run code to see test results.