Polynomial Kernel implementation.
Classes
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:
- Compute the dot product \langle x, y \rangle
-
Scale by
gammaand add the independent termcoef0 -
Raise the result to the power
degree
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
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 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.
See Also
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
compute_matrix_cross
(self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray
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.
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:
- Compute the unnormalized polynomial value K_{raw}(x, y)
- Compute self-similarities K_{raw}(x, x) and K_{raw}(y, y)
- 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 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.
See Also
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
compute_matrix_cross
(self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray
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.