Menu
Coddy logo textTech

predict method

Lesson 8 of 19 in Coddy's Introduction to Machine Learning course.

challenge icon

Challenge

Medium

Fill in the predict method.

The method should take a list of X new points.

For each new point, calculate the distance between this point and all other points stored in self.points. Extract the labels of the top self.k closest points.

Find the majority label from the closest points, this will be the predicted class.

For example here are our points and labels:

points(1, 5)(5, 0)(3, 2)(8, 1)(4, 4)(7, 7)
labelsABABCA

Here is a new point (3, 3)

The distance between this point and all other points is:

points(1, 5)(5, 0)(3, 2)(8, 1)(4, 4)(7, 7)
distance from (3, 3)813129232
labelsABABCA

Now if k = 3 then we need to find the 3 closest points to our new point. These are the closest: (3, 2), (1, 5), and (4, 4). The labels corresponding to these points are: A, A, C. We have 2 A's and 1 C. A is the majority, so the new point is classified as A.

The predict method should return a list of labels that were classified for the new points.

To find the majority label of a list of labels use: max(set(labels), key = labels.count)

Try it yourself

class KNN:
    def __init__(self, k):
        self.points = None
        self.labels = None
        self.k = k
    
    def distance(self, point_a, point_b):
        # Write your code here
        return (sum([(point_a[i] - point_b[i])**2 for i in range(len(point_a))]))**0.5
    
    def fit(self, X_train, y_train):
        # Write your code here
        self.points = X_train
        self.labels = y_train

    def predict(self, X_test):
        pass

All lessons in Introduction to Machine Learning