Sohrab Salimian
Sohrab Salimian

Reputation: 89

What is the Kernel for the KNN regressor in sklearn python?

Just wanted to know if anyone knows what the kernel is for the KNN regression in sklearn. I want to use a Gaussian kernel but I'm not sure if the kernel in the KNN regressor is Gaussian, any help on this topic would be greatly appreciated.

Upvotes: 2

Views: 2563

Answers (1)

mxbi
mxbi

Reputation: 893

I'm assuming you are talking about sklearn.neighbors.KNeighborsRegressor here.

It's a little unclear what you mean by 'kernel', but assuming you are talking about how the k neighbours are weighted:

  • By default, all K neighbours are weighted equally - a simple mean is taken disregarding the distances of the neighbours (weights='uniform')
  • You can set it to weight the neighbours based on the inverse of their distances when taking a mean by setting weights='distance' when creating the KNR object.

While a gaussian kernel is not implemented by default, KNR also has support for arbitrary functions which decide weights. You can define one like this:

def kernel(distances):
    # distances is an array of size K containing distances of neighbours
    weights = gaussian(distances) # Compute an array of weights however you want
return distances

and then passing this to your KNR initialising with weights=kernel. I recommend reading the sklearn docs for more info.

Upvotes: 2

Related Questions