Skip to Content
Hands-On Automated Machine Learning
book

Hands-On Automated Machine Learning

by Sibanjan Das, Umit Mert Cakmak
April 2018
Beginner to intermediate content levelBeginner to intermediate
282 pages
6h 52m
English
Packt Publishing
Content preview from Hands-On Automated Machine Learning

Implementing k-NN from scratch

You have already seen the k-NN algorithm in action; let's look at a very simple implementation. Save the following code block as knn_prediction.py:

import numpy as npimport operator# distance module includes various distance functions# You will use euclidean distance function to calculate distances between scoring input and training dataset.from scipy.spatial import distance# Decorating function with @profile to get run statistics@profiledef nearest_neighbors_prediction(x, data, labels, k):    # Euclidean distance will be calculated between example to be predicted and examples in data    distances = np.array([distance.euclidean(x, i) for i in data])    label_count = {}    for i in range(k): # Sorting distances starting ...
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

Automated Machine Learning

Automated Machine Learning

Adnan Masood
R: Unleash Machine Learning Techniques

R: Unleash Machine Learning Techniques

Raghav Bali, Dipanjan Sarkar, Brett Lantz, Cory Lesmeister

Publisher Resources

ISBN: 9781788629898Supplemental Content