RBF Kernel

~12 mincode completion

Implement rbf_kernel(x1, x2, gamma) that returns the RBF kernel value between two vectors.

Examples

Identical points: K = 1.0

Input
rbf_kernel([1, 2], [1, 2], 0.5)
Output
1

Unit distance, gamma=1: K = e^-1 ≈ 0.3679

Input
rbf_kernel([0, 0], [1, 0], 1)
Output
0.36788

Larger gamma: faster decay

Input
rbf_kernel([0, 0], [1, 0], 2)
Output
0.13534

Hints

Hint 1

applies elementwise, so negate the whole array and exponentiate it in one go.

Hint 2

Reach for squared rather than euclidean.

Requirements

  • x1: First vector

  • x2: Second vector

  • gamma: Kernel width parameter (gamma > 0)

  • Return Scalar kernel value in (0, 1].

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 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 rbf_kernel(x1: np.ndarray, x2: np.ndarray, gamma: float) -> float:
    """
    Compute the RBF (Gaussian) kernel between two vectors.

    Args:
        x1:    First vector
        x2:    Second vector
        gamma: Kernel width parameter (gamma > 0)

    Returns:
        Scalar kernel value in (0, 1].
    """
    # 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.