Files
mikoto10032--deeplearning/books/PRML/PRML-master-Python/prml/kernel/polynomial.py
T
2026-07-13 13:30:25 +08:00

44 lines
972 B
Python
Executable File

import numpy as np
from prml.kernel.kernel import Kernel
class PolynomialKernel(Kernel):
"""
Polynomial kernel
k(x,y) = (x @ y + c)^M
"""
def __init__(self, degree=2, const=0.):
"""
construct Polynomial kernel
Parameters
----------
const : float
a constant to be added
degree : int
degree of polynomial order
"""
self.const = const
self.degree = degree
def __call__(self, x, y, pairwise=True):
"""
calculate pairwise polynomial kernel
Parameters
----------
x : (..., ndim) ndarray
input
y : (..., ndim) ndarray
another input with the same shape
Returns
-------
output : ndarray
polynomial kernel
"""
if pairwise:
x, y = self._pairwise(x, y)
return (np.sum(x * y, axis=-1) + self.const) ** self.degree