saud - 5 months ago 28

Python Question

I am using scikit-learn's KNN regressor to fit a model to a large dataset with

`n_neighbors = 100-500`

`n_neighbors ~ 20-50`

Is there a way to vary the

`n_neighbors`

I could fit two models and stitch them together, but that would be inefficient. It would be preferable to either prescribe 2-3 values for

`n_neighbors`

`n_neighbors`

Answer

I'm afraid not. In part, this is due to some algebraic assumptions that the relationship is symmetric: A is a neighbour to B iff B is a neighbour to A. If you give different **k** values, you're guaranteed to break that symmetry.

I think the major reason is simply that the algorithm is simpler with a fixed quantity of neighbors, yielding better results *in general*. You have a specific case that KNN doesn't fit so well.

I suggest that you stitch together your two models, switching dependent on the imputed second derivative.