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

Class KernelRegression

numpy_ml/nonparametric/kernel_regression.py:4–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2
3
4class KernelRegression:
5 def __init__(self, kernel=None):
6 """
7 A Nadaraya-Watson kernel regression model.
8
9 Notes
10 -----
11 The Nadaraya-Watson regression model is
12
13 .. math::
14
15 f(x) = \sum_i w_i(x) y_i
16
17 where the sample weighting functions, :math:`w_i`, are simply
18
19 .. math::
20
21 w_i(x) = \\frac{k(x, x_i)}{\sum_j k(x, x_j)}
22
23 with `k` being the kernel function.
24
25 Observe that `k`-nearest neighbors
26 (:class:`~numpy_ml.nonparametric.KNN`) regression is a special case of
27 kernel regression where the `k` closest observations have a weight
28 `1/k`, and all others have weight 0.
29
30 Parameters
31 ----------
32 kernel : str, :doc:`Kernel <numpy_ml.utils.kernels>` object, or dict
33 The kernel to use. If None, default to
34 :class:`~numpy_ml.utils.kernels.LinearKernel`. Default is None.
35 """
36 self.parameters = {"X": None, "y": None}
37 self.hyperparameters = {"kernel": str(kernel)}
38 self.kernel = KernelInitializer(kernel)()
39
40 def fit(self, X, y):
41 """
42 Fit the regression model to the data and targets in `X` and `y`.
43
44 Parameters
45 ----------
46 X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, M)`
47 An array of N examples to generate predictions on
48 y : :py:class:`ndarray <numpy.ndarray>` of shape `(N, ...)`
49 Predicted targets for the `N` rows in `X`
50 """
51 self.parameters = {"X": X, "y": y}
52
53 def predict(self, X):
54 """
55 Generate predictions for the targets associated with the rows in `X`.
56
57 Parameters
58 ----------
59 X : :py:class:`ndarray <numpy.ndarray>` of shape `(N', M')`
60 An array of `N&#x27;` examples to generate predictions on
61

Callers 1

plot_regressionFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected