Page 22 - Data Science Algorithms in a Week
P. 22

Classification Using K Nearest Neighbors


            By applying this procedure to every data point, we can complete the graph as follows:


































            Note that sometimes a data point can be distanced from two known classes with the same
            distance: for example, 20 degrees Celsius and 6km/h. In such situations, we could prefer one
            class over the other or ignore these boundary cases. The actual result depends on the
            specific implementation of an algorithm.



            Implementation of k-nearest neighbors

            algorithm


            We implement the k-NN algorithm in Python to find Mary's temperature preference. In the
            end of this section we also implement the visualization of the data produced in example
            Mary and her temperature preferences by the k-NN algorithm. The full compilable code
            with the input files can be found in the source code provided with this book. The most
            important parts are extracted here:

                # source_code/1/mary_and_temperature_preferences/knn_to_data.py
                # Applies the knn algorithm to the input data.


                                                     [ 10 ]
   17   18   19   20   21   22   23   24   25   26   27