-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmv.py
More file actions
31 lines (23 loc) · 1.06 KB
/
mv.py
File metadata and controls
31 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# Load the movie dataset
movies = pd.read_csv("movies.csv")
# Calculate the cosine similarity between all movie pairs
similarity = cosine_similarity(movies.iloc[:,1:])
# Write a function to get recommendations for a given movie
def recommend_movies(title, similarity=similarity):
# Get the index of the movie
index = movies[movies.title == title].index[0]
# Get the pairwise similarity scores of all movies with that movie
scores = list(enumerate(similarity[index]))
# Sort the movies based on the similarity scores
scores = sorted(scores, key=lambda x: x[1], reverse=True)
# Get the indices of the top 5 most similar movies (excluding the movie itself)
scores = scores[1:6]
# Return the titles of the most similar movies
movie_indices = [i[0] for i in scores]
return movies['title'].iloc[movie_indices]
# Test the recommendation function with a sample movie
print(recommend_movies("The Matrix"))