Skip to Content
Practical Data Analysis Cookbook
book

Practical Data Analysis Cookbook

by Tomasz Drabas
April 2016
Beginner to intermediate content levelBeginner to intermediate
384 pages
8h 36m
English
Packt Publishing
Content preview from Practical Data Analysis Cookbook

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Data Analysis Cookbook

Python Data Analysis Cookbook

Ivan Idris
Practical Simulations for Machine Learning

Practical Simulations for Machine Learning

Paris Buttfield-Addison, Mars Buttfield-Addison, Tim Nugent, Jon Manning

Publisher Resources

ISBN: 9781783551668Supplemental Content