Implementation of the k-nearest neighbors algorithm

Now, we will implement the k-NN algorithm in Python to find Mary's temperature preference. At the end of this section, we will also implement the visualization of the data produced in the previous section, that is, Mary and her temperature preferences. The full, compilable code, with the input files, can be found in the source code provided with this book. The most important parts have been extracted and presented here:

# source_code/1/mary_and_temperature_preferences/knn_to_data.py# Applies the knn algorithm to the input data.# The input text file is assumed to be of the format with one line per# every data entry consisting of the temperature in degrees Celsius,# wind speed and then the ...

Get Data Science Algorithms in a Week - Second Edition 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.