API Reference / algorithms / svm / kernels /

polynomial.py

Polynomial Kernel implementation.

Classes

PolynomialKernel

class algorithms.svm.kernels.polynomial.PolynomialKernel(CachedKernel)

Polynomial Kernel computing a polynomial of the dot product.

The Polynomial Kernel maps input vectors into a feature space spanned by all monomials up to degree d, enabling SVMs to learn polynomial decision boundaries without explicitly computing the high-dimensional feature map.
Constructor
__init__(
    self,
    degree: int = 3,
    gamma: float = 1.0,
    coef0: float = None,
    lower_order: bool = True,
    cache_size: int = 250007,
)

Overview

The kernel evaluation proceeds as follows:

  1. Compute the dot product \langle x, y \rangle
  2. Scale by gamma and add the independent term coef0
  3. Raise the result to the power degree
When coef0 > 0 (lower_order=True), the implicit feature map includes all monomials of degree \leq d; when coef0 = 0, only degree-d monomials are included.

Theory

The polynomial kernel function is defined as:

K(x, y) = (\gamma \, \langle x, y \rangle + c_0)^d
where:
  • \gamma --- Scaling coefficient for the dot product
  • c_0 --- Independent (bias) term controlling lower-order contributions
  • d --- Degree of the polynomial
For c_0 = 0 this is a homogeneous polynomial kernel; for c_0 > 0 it is inhomogeneous, including cross-terms of all orders up to d.

Parameters

degree
int = 3
Degree of the polynomial.
gamma
float = 1.0
Scaling coefficient for the dot product.
coef0
float or None = None
Independent term. Defaults to 1.0 if lower_order is True, else 0.0.
lower_order
bool = True
Whether to include lower-order polynomial terms (sets coef0 default).
cache_size
int = 250007
Maximum number of cached kernel evaluations.

Attributes

X\_
np.ndarray
Training data stored after build().
n_samples\_
int
Number of training samples.
n_features\_
int
Number of features observed during build().

Notes

Complexity:

  • Single evaluation: O(p) where p = number of features
  • Matrix computation: O(n^2 p) for n samples
When to use PolynomialKernel:
  • When polynomial interactions among features are known or suspected
  • Image recognition tasks where higher-order feature interactions matter
  • When the RBF kernel overfits and a more constrained feature space is preferred
  • Degree 1 reduces to the linear kernel; degree 2-3 is common in practice

References

Scholkopf2002
Schoelkopf, B. and Smola, A.J. (2002). Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond. MIT Press.

Basic usage with a cubic polynomial kernel:

python
>>> from tuiml.algorithms.svm.kernels import PolynomialKernel
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4]])
>>> kernel = PolynomialKernel(degree=3, lower_order=True)
>>> kernel.build(X_train)
PolynomialKernel(...)
>>> value = kernel.evaluate(X_train[0], X_train[1])

Methods

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema.

evaluate (self, x1: np.ndarray, x2: np.ndarray) -> float

Evaluate polynomial kernel.

Parameters
x1
np.ndarray
First vector.
x2
np.ndarray
Second vector.
Returns
val
float
Polynomial kernel value.
compute_matrix (self) -> np.ndarray

Compute the polynomial kernel matrix using vectorized operations.

Returns
K
np.ndarray of shape (n_samples, n_samples)
The kernel (Gram) matrix.
compute_matrix_cross (self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray

Compute the polynomial kernel matrix between two sample sets.

Parameters
X1
np.ndarray of shape (n1, n_features)
First set of samples.
X2
np.ndarray of shape (n2, n_features)
Second set of samples.
Returns
K
np.ndarray of shape (n1, n2)
Kernel matrix.
__repr__ (self) -> str

String representation.

NormalizedPolynomialKernel

class algorithms.svm.kernels.polynomial.NormalizedPolynomialKernel(CachedKernel)

Normalized Polynomial Kernel producing values in [0, 1].

A polynomial kernel normalized by the geometric mean of the self-similarities, ensuring that K(x, x) = 1 for all x. This removes the influence of vector magnitude and focuses purely on angular relationships.
Constructor
__init__(
    self,
    degree: int = 2,
    lower_order: bool = True,
    cache_size: int = 250007,
)

Overview

The kernel evaluation proceeds as follows:

  1. Compute the unnormalized polynomial value K_{raw}(x, y)
  2. Compute self-similarities K_{raw}(x, x) and K_{raw}(y, y)
  3. Divide by \sqrt{K_{raw}(x, x) \cdot K_{raw}(y, y)}

Theory

The normalized polynomial kernel is defined as:

K_{norm}(x, y) = \frac{(\langle x, y \rangle + c_0)^d}{\sqrt{(\langle x, x \rangle + c_0)^d \cdot (\langle y, y \rangle + c_0)^d}}

This ensures K_{norm}(x, x) = 1 and |K_{norm}(x, y)| \leq 1.

Parameters

degree
int = 2
Degree of the polynomial.
lower_order
bool = True
Whether to include lower-order terms (controls coef0).
cache_size
int = 250007
Maximum number of cached kernel evaluations.

Attributes

X\_
np.ndarray
Training data stored after build().
n_samples\_
int
Number of training samples.

Notes

Complexity:

  • Single evaluation: O(p) where p = number of features (three dot products)
  • Matrix computation: O(n^2 p) for n samples
When to use NormalizedPolynomialKernel:
  • When input vectors have varying magnitudes and normalization is desired
  • As a drop-in replacement for the standard polynomial kernel with better numerical stability
  • When kernel values should be bounded in [0, 1]

References

Scholkopf2002
Schoelkopf, B. and Smola, A.J. (2002). Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond. MIT Press.

Basic usage with a quadratic normalized kernel:

python
>>> from tuiml.algorithms.svm.kernels import NormalizedPolynomialKernel
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4]])
>>> kernel = NormalizedPolynomialKernel(degree=2)
>>> kernel.build(X_train)
NormalizedPolynomialKernel(...)
>>> value = kernel.evaluate(X_train[0], X_train[1])

Methods

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema.

evaluate (self, x1: np.ndarray, x2: np.ndarray) -> float

Evaluate normalized polynomial kernel.

Parameters
x1
np.ndarray
First vector.
x2
np.ndarray
Second vector.
Returns
val
float
Normalized polynomial kernel value.
compute_matrix (self) -> np.ndarray

Compute the normalized polynomial kernel matrix.

Returns
K
np.ndarray of shape (n_samples, n_samples)
The kernel (Gram) matrix.
compute_matrix_cross (self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray

Compute the normalized polynomial kernel matrix between two sample sets.

Parameters
X1
np.ndarray of shape (n1, n_features)
First set of samples.
X2
np.ndarray of shape (n2, n_features)
Second set of samples.
Returns
K
np.ndarray of shape (n1, n2)
Kernel matrix.
__repr__ (self) -> str

String representation.