📘
RBF Kernel Matrix
Compute the Radial Basis Function (RBF/Gaussian) kernel matrix.
The RBF kernel is: K(x,y)=exp(−2σ2∥x−y∥2)
Given a set of vectors X (n points) and parameter σ, compute the n×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(−2σ2∥x−y∥2) for each pair of points x and y in the input set X.
- For the given input X=[[0,0],[1,0],[0,1]] and σ=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.0 since ∥x−x∥2=0, resulting in K(x,x)=exp(0)=1.0 for each point x.
- The off-diagonal elements are calculated using the RBF kernel formula, e.g., K([0,0],[1,0])=exp(−2⋅1.02∥(0−1)2+(0−0)2∥)=exp(−21)≈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
Python 3.13.1
Test Results
0/0Run code to see test results.