Employing the kNN model in a regression problem
Although used predominantly to solve classification problems, the k-Nearest Neighbors model that we saw in Chapter 3, Classification Techniques, can also be used in regression models. This recipe will teach you how it can be applied.
Getting ready
To execute this recipe, you will need pandas
and Scikit
. No other prerequisites are required.
How to do it…
Again, using Scikit
to estimate this model is extremely simple (the regression_knn.py
file):
import sklearn.neighbors as nb @hlp.timeit def regression_kNN(x,y): ''' Build the kNN classifier ''' # create the classifier object knn = nb.KNeighborsRegressor(n_neighbors=80, algorithm='kd_tree', n_jobs=-1) # fit the data knn.fit(x,y) # return the classifier return knn
How it works…
First, we read the data in and split it into the dependent variable y
and independent variables x_sig
; we are selecting only the significant variables that we found earlier,...