def recommend_movies(movie_query, k_recommendations): raw_movies_data = [] with open('movies_recommendation_data.csv', 'r') as md: # Discard the first line (headings) next(md) # Read the data into memory for line in md.readlines(): data_row = line.strip().split(',') raw_movies_data.append(data_row) # Prepare the data for use in the knn algorithm by picking # the relevant columns and converting the numeric columns # to numbers since they were read in as strings movies_recommendation_data = [] for row in raw_movies_data: data_row = list(map(float, row[2:])) movies_recommendation_data.append(data_row) # Use the KNN algorithm to get the 5 movies that are most # similar to The Post. recommendation_indices, _ = knn(movies_recommendation_data, movie_query, k=k_recommendations, distance_fn=euclidean_distance, choice_fn=lambda x: None) movie_recommendations = [] for _, index in recommendation_indices: movie_recommendations.append(raw_movies_data[index]) return movie_recommendations
def FindBestwithKNN(restaurant_query, k_recommendations, dataset): recommendation_indices, _ = knn(dataset, restaurant_query, k=k_recommendations, distance_fn=euclidean_distance, choice_fn=lambda x: None) restaurant_recommendations = [] for _, index in recommendation_indices: restaurant_recommendations.append(dataset[index]) return restaurant_recommendations
def recommend_movies(query,num_of_recommendation): raw_movies_data = [] with open('movies_recommendation_data.csv','r') as md: # Discard the first line which is the heading next(md) for line in md.readlines(): data_row = line.strip().split(',') raw_movies_data.append(data_row) movies_recommmendation_data = [] for row in raw_movies_data: data_row = list(map(float,row[2:])) movies_recommmendation_data.append(data_row) # Use knn to get the k movie recommendation recommend_indices,_ = knn(movies_recommmendation_data,query,num_of_recommendation,euclidean_distance,lambda x: None) movie_recommendations = [] for _,index in recommend_indices: movie_recommendations.append(raw_movies_data[index]) return movie_recommendations
def recommend_movies(movie_query, k_recommendations): raw_movies_data = [] # open file, read only with open('movies_recommendation_data.csv', 'r') as md: next(md) # skip the 'first line' # read data into memory for line in md: data_row = line.strip().split(',') # debug print("\n") print("data_row", data_row) raw_movies_data.append(data_row) # debug print("\n\n") print("raw_movies_data", raw_movies_data) # prepare the data, so it can be use by the 'KNN' # need to convert data in 'each column' into numbers # because it was 'read in' as a string movies_recommendation_data = [] for row in raw_movies_data: # row[2:], get data 'starting' from 'row 2' # probably don't need ' list() ' data_row = list(map(float, row[2:])) movies_recommendation_data.append(data_row) # debug print("\n\n") print("movies_recommendation_data", movies_recommendation_data) print("\n") print("type movies_recommendation_data", type(movies_recommendation_data) ) # use KNN algorithm to get '5 similar movies' to 'query_movie' recommendation_indices, jim = knn( movies_recommendation_data, movie_query, k=k_recommendations, distance_fn=euclidean_distance, choice_fn=lambda x: None ) # debug print("\n\n") print("recommendation_indices", recommendation_indices) movie_recommendations = [] for _, index in recommendation_indices: # debug print("\n\n") print("_", _) print("index", index) movie_recommendations.append(raw_movies_data[index]) # debug print("\n\n") print("movie_recommendations", movie_recommendations) return movie_recommendations