April 2016
Beginner to intermediate
384 pages
8h 36m
English
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.
To execute this recipe, you will need pandas and Scikit. No other prerequisites are required.
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 ...