Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
69 lines (51 loc) · 2.48 KB
/
Copy pathscript.py
File metadata and controls
69 lines (51 loc) · 2.48 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Description: This file contains the code to get movie recommendations based on the movie name.
import argparse
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
parser = argparse.ArgumentParser(description='Get movie recommendations.')
parser.add_argument('movie_name', type=str, help='Name of the movie to get recommendations for')
args = parser.parse_args()
# Function to clean data
def clean_data(x):
return str.lower(x.replace(" ", " "))
# Function to create a combined feature
def create_soup(x):
return x['title'] + ' ' + x['director'] + ' ' + x['cast'] + ' ' + x['listed_in'] + ' ' + x['description']
# Function to get movie recommendations
def get_recommendations(cosine_sim, netflix_data):
sim_scores = list(enumerate(cosine_sim[0]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:9] # Exclude the movie itself
movie_indices = [i[0] for i in sim_scores]
result = netflix_data['title'].iloc[movie_indices].tolist()
return result
if __name__ == "__main__":
# Load data
netflix_overall = pd.read_csv('netflix_titles.csv').fillna('')
netflix_data = netflix_overall[['title', 'director', 'cast', 'listed_in', 'description']]
# Clean data and create combined feature
for feature in netflix_data.columns:
netflix_data = netflix_data.assign(**{feature: netflix_data[feature].apply(clean_data)})
# Create combined feature using .apply with axis=1
soup_df = netflix_data.apply(lambda row: create_soup(row), axis=1)
# Create a new DataFrame with the 'soup' column
netflix_data = netflix_data.assign(soup=soup_df)
# Reset index and create series for easy indexing
netflix_data = netflix_data.reset_index()
indices = pd.Series(netflix_data.index, index=netflix_data['title'])
title=clean_data(args.movie_name)
if title not in indices:
print(["No recommendations available for the provided movie name."])
exit(0)
movie_index=indices[title]
# Create CountVectorizer and calculate cosine similarity
count = CountVectorizer(stop_words='english')
count_matrix = count.fit_transform(netflix_data['soup'])
user_count_matrix= count_matrix[movie_index]
cosine_sim2 = cosine_similarity(user_count_matrix, count_matrix)
# Get movie recommendations and print
def predict_movie():
return get_recommendations(cosine_sim2, netflix_data)
# test
print(predict_movie())