| 117 | |
| 118 | |
| 119 | class PolynomialKernel(KernelBase): |
| 120 | def __init__(self, d=3, gamma=None, c0=1): |
| 121 | """ |
| 122 | The degree-`d` polynomial kernel. |
| 123 | |
| 124 | Notes |
| 125 | ----- |
| 126 | For input vectors :math:`\mathbf{x}` and :math:`\mathbf{y}`, the polynomial |
| 127 | kernel is: |
| 128 | |
| 129 | .. math:: |
| 130 | |
| 131 | k(\mathbf{x}, \mathbf{y}) = (\gamma \mathbf{x}^\\top \mathbf{y} + c_0)^d |
| 132 | |
| 133 | In contrast to the linear kernel, the polynomial kernel also computes |
| 134 | similarities *across* dimensions of the **x** and **y** vectors, |
| 135 | allowing it to account for interactions between features. As an |
| 136 | instance of the dot product family of kernels, the polynomial kernel is |
| 137 | invariant to a rotation of the coordinates about the origin, but *not* |
| 138 | to translations. |
| 139 | |
| 140 | Parameters |
| 141 | ---------- |
| 142 | d : int |
| 143 | Degree of the polynomial kernel. Default is 3. |
| 144 | gamma : float or None |
| 145 | A scaling parameter for the dot product between `x` and `y`, |
| 146 | determining the amount of smoothing/resonlution of the kernel. |
| 147 | Larger values result in greater smoothing. If None, defaults to 1 / |
| 148 | `C`. Sometimes referred to as the kernel bandwidth. Default is |
| 149 | None. |
| 150 | c0 : float |
| 151 | Parameter trading off the influence of higher-order versus lower-order |
| 152 | terms in the polynomial. If `c0` = 0, the kernel is said to be |
| 153 | homogenous. Default is 1. |
| 154 | """ |
| 155 | super().__init__() |
| 156 | self.hyperparameters = {"id": "PolynomialKernel"} |
| 157 | self.parameters = {"d": d, "c0": c0, "gamma": gamma} |
| 158 | |
| 159 | def _kernel(self, X, Y=None): |
| 160 | """ |
| 161 | Compute the degree-`d` polynomial kernel between all pairs of rows in `X` |
| 162 | and `Y`. |
| 163 | |
| 164 | Parameters |
| 165 | ---------- |
| 166 | X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, C)` |
| 167 | Collection of `N` input vectors |
| 168 | Y : :py:class:`ndarray <numpy.ndarray>` of shape `(M, C)` or None |
| 169 | Collection of `M` input vectors. If None, assume `Y = X`. Default |
| 170 | is None. |
| 171 | |
| 172 | Returns |
| 173 | ------- |
| 174 | out : :py:class:`ndarray <numpy.ndarray>` of shape `(N, M)` |
| 175 | Similarity between `X` and `Y` where index (`i`, `j`) gives |
| 176 | :math:`k(x_i, y_j)` (i.e., the kernel's Gram-matrix). |
no outgoing calls