KernelRegressionNW
Nadaraya-Watson kernel regression is a non-parametric technique used for estimating the conditional expectation of a random variable. It works by placing a kernel function at each training data point and computing a weighted average of the target values, where weights are determined by the proximity of the query point to each training point.
This class is a simple implementation of the Nadaraya-Watson kernel regression estimator for usage with scikit-learn.
Bases: MultiOutputMixin, RegressorMixin, BaseEstimator
Source code in src/nadaraya_watson/kernel_regression.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
__init__(*, bandwidth=1.0, kernel='gaussian', metric='euclidean')
¶
Initialize the Nadaraya-Watson Kernel regression estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth
|
float or str
|
The bandwidth of the kernel. If a string, it must be one of "scott" or "silverman". |
1.0
|
kernel
|
str
|
The kernel to use. Must be one of the valid kernels. |
"gaussian"
|
metric
|
str
|
The distance metric to use. Must be one of the valid metrics. |
"euclidean"
|
Source code in src/nadaraya_watson/kernel_regression.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
fit(X, y, sample_weight=None)
¶
Fit the Nadaraya-Watson Kernel regression estimator on the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array - like
|
array of shape (n_samples, n_features) Training data. |
required |
y
|
array - like
|
array of shape (n_samples,) or (n_samples, n_targets) Target values. |
required |
sample_weight
|
array - like
|
array of shape (n_samples,) Individual weights for each sample; ignored if None is passed. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
object
|
Returns the instance itself. |
Source code in src/nadaraya_watson/kernel_regression.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
predict(X)
¶
Predict using Nadaraya-Watson Kernel regression estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array - like
|
array of shape (n_samples, n_features) Samples. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
y_prediction |
ndarray
|
array of shape (n_samples,) or (n_samples, n_targets) Returns predicted values. |
Source code in src/nadaraya_watson/kernel_regression.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
valid_kernels()
classmethod
¶
Return a list of valid kernels.
Returns:
| Type | Description |
|---|---|
list of str
|
A list of valid kernel names. |
Source code in src/nadaraya_watson/kernel_regression.py
64 65 66 67 68 69 70 71 72 73 | |
valid_metrics()
classmethod
¶
Return a list of valid metrics.
Please note that some names are actually synonymous. Please do not feed these values directly to sklearn for grid-search cross-validation.
e.g. "euclidean" and "l2" are identical.
Returns:
| Type | Description |
|---|---|
list of str
|
A list of valid metric names. |
Source code in src/nadaraya_watson/kernel_regression.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |