Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 9, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/9d82adbb95dfb1ce47f6869394178395 to your computer and use it in GitHub Desktop.
Save codecademydev/9d82adbb95dfb1ce47f6869394178395 to your computer and use it in GitHub Desktop.
Codecademy export
from movies import movie_dataset, movie_ratings
def distance(movie1, movie2):
squared_difference = 0
for i in range(len(movie1)):
squared_difference += (movie1[i] - movie2[i]) ** 2
final_distance = squared_difference ** 0.5
return final_distance
def predict(unknown, dataset, movie_ratings, k):
distances = []
#Looping through all points in the dataset
for title in dataset:
movie = dataset[title]
distance_to_point = distance(movie, unknown)
#Adding the distance and point associated with that distance
distances.append([distance_to_point, title])
distances.sort()
#Taking only the k closest points
neighbors = distances[0:k]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment