From the course: Supervised Learning Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Building your own KNN

Building your own KNN - Python Tutorial

From the course: Supervised Learning Essential Training

Start my 1-month free trial

Building your own KNN

- [Instructor] The first step to creating a k-NN is to load the training and testing data. Remember when I said k-NNs are computationally expensive? Next, we need to choose the value of k, i.e. the nearest data points. k can be any integer. And one of the ways we can initially search for k is by looking at the clusters in our scatter plot. For this project, let's set our first k to three and see what we get. For each individual point in the test data, we have to calculate the distance between the test data and every row of the training data. Based on this distance, we sort the training points in ascending order. We'll then use the top three training points, since our k was set to three. Of those closest points, the most frequent class is chosen and is set as the new example class. For regression problems, the values from these examples are averaged to assign a new value to the new training example. In our notebook…

Contents