-
-
Save codecademydev/9d82adbb95dfb1ce47f6869394178395 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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