MCPcopy
hub / github.com/ddbourgin/numpy-ml / LinearKernel

Class LinearKernel

numpy_ml/utils/kernels.py:72–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70
71
72class LinearKernel(KernelBase):
73 def __init__(self, c0=0):
74 """
75 The linear (i.e., dot-product) kernel.
76
77 Notes
78 -----
79 For input vectors :math:`\mathbf{x}` and :math:`\mathbf{y}`, the linear
80 kernel is:
81
82 .. math::
83
84 k(\mathbf{x}, \mathbf{y}) = \mathbf{x}^\\top \mathbf{y} + c_0
85
86 Parameters
87 ----------
88 c0 : float
89 An "inhomogeneity" parameter. When `c0` = 0, the kernel is said to be
90 homogenous. Default is 1.
91 """
92 super().__init__()
93 self.hyperparameters = {"id": "LinearKernel"}
94 self.parameters = {"c0": c0}
95
96 def _kernel(self, X, Y=None):
97 """
98 Compute the linear kernel (i.e., dot-product) between all pairs of rows in
99 `X` and `Y`.
100
101 Parameters
102 ----------
103 X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, C)`
104 Collection of `N` input vectors
105 Y : :py:class:`ndarray <numpy.ndarray>` of shape `(M, C)` or None
106 Collection of `M` input vectors. If None, assume `Y` = `X`.
107 Default is None.
108
109 Returns
110 -------
111 out : :py:class:`ndarray <numpy.ndarray>` of shape `(N, M)`
112 Similarity between `X` and `Y`, where index (`i`, `j`) gives
113 :math:`k(x_i, y_j)`.
114 """
115 X, Y = kernel_checks(X, Y)
116 return X @ Y.T + self.parameters["c0"]
117
118
119class PolynomialKernel(KernelBase):

Callers 4

test_linear_kernelFunction · 0.90
__call__Method · 0.85
init_from_strMethod · 0.85
init_from_dictMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_linear_kernelFunction · 0.72