KernelRegression

class numpy_ml.nonparametric.KernelRegression(kernel=None)[source]

A Nadaraya-Watson kernel regression model.

Notes

The Nadaraya-Watson regression model is

\[f(x) = \sum_i w_i(x) y_i\]

where the sample weighting functions, \(w_i\), are simply

\[w_i(x) = \frac{k(x, x_i)}{\sum_j k(x, x_j)}\]

with k being the kernel function.

Observe that k-nearest neighbors (KNN) regression is a special case of kernel regression where the k closest observations have a weight 1/k, and all others have weight 0.

Parameters:kernel (str, Kernel object, or dict) – The kernel to use. If None, default to LinearKernel. Default is None.
fit(X, y)[source]

Fit the regression model to the data and targets in X and y.

Parameters:
  • X (ndarray of shape (N, M)) – An array of N examples to generate predictions on
  • y (ndarray of shape (N, …)) – Predicted targets for the N rows in X
predict(X)[source]

Generate predictions for the targets associated with the rows in X.

Parameters:X (ndarray of shape (N’, M’)) – An array of N’ examples to generate predictions on
Returns:y (ndarray of shape (N’, …)) – Predicted targets for the N’ rows in X