<jupyter_start><jupyter_text>Movie Recommendation System<jupyter_code>import numpy as np 
import pandas as pd
import ast
movies=pd.read_csv('tmdb_5000_movies.csv')
credits=pd.read_csv('tmdb_5000_credits.csv')
movies.head()
movies=movies.merge(credits,on='title')<jupyter_output><empty_output><jupyter_text>Enlisting criterias for movie tags<jupyter_code>#movie_id
#title
#overview
#genres
#keywords
#cast
#crew
movies = movies[['movie_id','title','overview','genres','keywords','cast','crew']]
movies.head()
movies.isnull().sum()
movies.dropna(inplace=True)
movies.duplicated().sum()
movies.iloc[0].genres
# '[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 14, "name": "Fantasy"}, {"id": 878, "name": "Science Fiction"}]'
def convert(obj):
    M=[]
    for i in ast.literal_eval(obj):
        M.append(i['name'])
    return M    
movies['genres']=movies['genres'].apply(convert)
movies.head()
movies['keywords'] = movies['keywords'].apply(convert)
movies.head()
def convert5(obj):
    L=[]
    counter=0
    for i in ast.literal_eval(obj):
        if counter!=5:
            L.append(i['name'])
            counter+=1
        else:
            break
    return L   
movies['cast'] = movies['cast'].apply(convert5)
movies.head()
def fetch_director(obj):
    L=[]
    counter=0
    for i in ast.literal_eval(obj):
        if i['job']=='Director':
            L.append(i['name'])
            break
    return L   
movies['crew'] = movies['crew'].apply(fetch_director)
movies.head()
movies['overview'] = movies['overview'].apply(lambda x:x.split())
movies.head()
movies['genres']=movies['genres'].apply(lambda x:[i.replace(" ","") for i in x])
movies['keywords']=movies['keywords'].apply(lambda x:[i.replace(" ","") for i in x])
movies['crew']=movies['crew'].apply(lambda x:[i.replace(" ","") for i in x])
movies['cast']=movies['cast'].apply(lambda x:[i.replace(" ","") for i in x])
movies.head()
movies['tags']= movies['overview']+movies['genres']+movies['keywords']+movies['cast']+movies['crew']
movies.head()
dataframe=movies[['movie_id','title','tags']]
dataframe['tags']=dataframe['tags'].apply(lambda x:" ".join(x))
dataframe.head()
dataframe['tags']=dataframe['tags'].apply(lambda x:x.lower())<jupyter_output>C:\Users\gagro\AppData\Local\Temp/ipykernel_5548/259234246.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  dataframe['tags']=dataframe['tags'].apply(lambda x:x.lower())<jupyter_text>Final Dataframe Obtained<jupyter_code>dataframe.head()
import nltk
from nltk.stem.porter import PorterStemmer
ps=PorterStemmer()
def stem(text):
    y=[]
    for i in text.split():
        y.append(ps.stem(i))
        
    return " ".join(y)     
dataframe['tags'] = dataframe['tags'].apply(stem)
from sklearn.feature_extraction.text import CountVectorizer
cv=CountVectorizer(max_features=5000,stop_words='english')
vectors=cv.fit_transform(dataframe['tags']).toarray()
cv.get_feature_names()
from sklearn.metrics.pairwise import cosine_similarity 
similarity =  cosine_similarity(vectors)
def recommend(movie):
    movie_index = dataframe[dataframe['title'] == movie].index[0]
    distances=similarity[movie_index]
    movie_list=sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[1:6]
    
    for i in movie_list:
        print(dataframe.iloc[i[0]].movie_id)<jupyter_output><empty_output><jupyter_text>Creating final files for Application Use<jupyter_code>import pickle
pickle.dump(dataframe.to_dict(),open('movie_dict.pkl','wb'))
pickle.dump(similarity,open('similarity.pkl','wb'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17944664031620552, 'fraction_numerical': 0.01238471673254282, 'mean_word_length': 5.851985559566787, 'pattern_counts': {'":': 8, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '117324', 'license': 'MIT', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1352, 'n_words': 226, 'path': 'movie-recommendation-system.ipynb', 'repo_name': 'gagroo-jeevansh/Movie-Recommendation-System', 'size': '3795'}