August 6, 2020

K-Nearest Neighbors Classifier

  1. KNN is a classification algorithm. Data points with similar attributes fall into similar categories.

  2. Three steps of the KNN algorithm:

    1. Normalize the data.

    2. Find the K-Nearest Neighbors.

    3. Classify the new point based on those neighbors.

  3. Finding K-Nearest Neighbors.

    1. def distance(movie1, movie2):

      1. squared_difference = 0

      2. for i in range(len(movie1)):

        1. squared_difference += (movie1[i] - movie2[i]**2

      3. final_distance = squared_distance ** 0.5

      4. return final_distance

    2. def classify(unknown, dataset, k):

      1. distances = [ ]

      2. for title in dataset:

        1. movie = dataset[title]

        2. distance_to_point = distance(movie, unknown)

        3. #adding the distance and point associated

        4. distances.append([distance_to_point, title])

      3. distances.sort( ) #using Python’s sort function

      4. neighbors = distances[0:k] #only the k closest points

      5. return neighbors

Previous
Previous

August 11, 2020

Next
Next

August 5, 2020