August 6, 2020
K-Nearest Neighbors Classifier
KNN is a classification algorithm. Data points with similar attributes fall into similar categories.
Three steps of the KNN algorithm:
Normalize the data.
Find the K-Nearest Neighbors.
Classify the new point based on those neighbors.
Finding K-Nearest Neighbors.
def distance(movie1, movie2):
squared_difference = 0
for i in range(len(movie1)):
squared_difference += (movie1[i] - movie2[i]**2
final_distance = squared_distance ** 0.5
return final_distance
def classify(unknown, dataset, k):
distances = [ ]
for title in dataset:
movie = dataset[title]
distance_to_point = distance(movie, unknown)
#adding the distance and point associated
distances.append([distance_to_point, title])
distances.sort( ) #using Python’s sort function
neighbors = distances[0:k] #only the k closest points
return neighbors