Skip to main content
Kernel functions enable SVM classifiers to operate in high-dimensional feature spaces without explicitly computing the transformed features. This page documents the base kernel interface and common kernel types.

KernelFunction

Type-erased value-semantic kernel handle that provides copyable, movable, and polymorphic kernel functionality.

Constructor

impl
std::unique_ptr<Kernel>
Unique pointer to a concrete kernel implementation

Methods

operator()

Evaluates the kernel function on two input vectors.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Kernel evaluation result k(x, y)

valid()

Checks if the kernel function is initialized.
return
bool
True if the kernel contains a valid implementation, false otherwise

LinearKernel

Linear kernel corresponding to the identity feature map in ℝⁿ. Formula: k(x, y) = ⟨x, y⟩ The linear kernel computes the standard dot product between two vectors. It is the simplest kernel and corresponds to no transformation of the feature space.

Constructor

Default constructor with no parameters.

Methods

operator()

Computes the dot product between two vectors.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Dot product ⟨x, y⟩

Example

#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Create a linear kernel
LinearKernel linear;

// Create kernel function wrapper
KernelFunction kernel(std::make_unique<LinearKernel>());

// Evaluate on two vectors
Vector x = {1.0, 2.0, 3.0};
Vector y = {4.0, 5.0, 6.0};

double result = kernel(x, y);  // Returns 32.0 (1*4 + 2*5 + 3*6)

PolynomialKernel

Polynomial kernel that generates a finite-dimensional RKHS with dimension growing combinatorially with the degree. Formula: k(x, y) = (γ ⟨x, y⟩ + c)^d

Constructor

gamma
double
Scale parameter γ for the dot product
coef0
double
Independent coefficient c (offset term)
degree
std::size_t
Polynomial degree d

Methods

operator()

Computes the polynomial kernel between two vectors.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Polynomial kernel value (γ ⟨x, y⟩ + c)^d

Example

#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Create a polynomial kernel of degree 3
// with gamma=1.0 and offset=1.0
PolynomialKernel poly(1.0, 1.0, 3);

KernelFunction kernel(std::make_unique<PolynomialKernel>(1.0, 1.0, 3));

Vector x = {1.0, 2.0};
Vector y = {3.0, 4.0};

double result = kernel(x, y);  // (1*3 + 2*4 + 1)^3 = 12^3 = 1728

RBFKernel

Radial Basis Function (Gaussian) kernel that induces an infinite-dimensional, separable RKHS. Universally consistent under mild conditions. Formula: k(x, y) = exp(-γ ||x - y||²) The RBF kernel is one of the most popular kernels in practice due to its smoothness properties and universal approximation capabilities.

Constructor

gamma
double
Bandwidth parameter γ controlling the kernel width. Larger values lead to more localized influence.

Methods

operator()

Computes the RBF kernel between two vectors.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
RBF kernel value exp(-γ ||x - y||²)

Example

#include <mlpp/classifiers/kernel/rkhs_kernels.hpp>

using namespace mlpp::classifiers::kernel;

// Create an RBF kernel with gamma=0.5
RBFKernel rbf(0.5);

KernelFunction kernel(std::make_unique<RBFKernel>(0.5));

Vector x = {1.0, 2.0, 3.0};
Vector y = {1.5, 2.5, 3.5};

double result = kernel(x, y);  // exp(-0.5 * 0.75) ≈ 0.687

Kernel base class

Abstract base class for all kernel implementations.

Methods

operator()

Pure virtual method for kernel evaluation.
x
const Vector&
First input vector
y
const Vector&
Second input vector
return
double
Kernel evaluation k(x, y)

clone()

Creates a deep copy of the kernel.
return
std::unique_ptr<Kernel>
Unique pointer to a cloned kernel instance

Type aliases

  • Vector = std::vector<double> - Standard vector type used throughout the kernel API

Build docs developers (and LLMs) love