April 2018
Beginner to intermediate
282 pages
6h 52m
English
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 ...