forked from Abhishek4848/Moviebot-NLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoviereccomendertest.py
44 lines (35 loc) · 1.14 KB
/
moviereccomendertest.py
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
# In[2]:
def cosine_recommendations(title):
movies = pd.read_csv('movies.csv')
from sklearn.feature_extraction.text import TfidfVectorizer
tf = TfidfVectorizer(analyzer='word',ngram_range=(1, 2),min_df=0, stop_words='english')
tfidf_matrix = tf.fit_transform(movies['genres'])
from sklearn.metrics.pairwise import linear_kernel
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
titles = movies['title']
indices = pd.Series(movies.index, index=movies['title'])
idx = indices[title]
sim_scores = list(enumerate(cosine_sim[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:21]
movie_indices = [i[0] for i in sim_scores]
l = list(titles.iloc[movie_indices].head(5))
return l
def failsafe(title):
df = pd.read_csv('movies.csv')
l = list(df['title'])
if title not in l:
flag = 'Fail'
return flag
else:
flag = 'pass'
<<<<<<< HEAD
return flag
=======
return flag
>>>>>>> f87f0d7273d38c8160da02a377e2b38a4f1867d4