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 ...

Get Practical Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.