# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

movies = pd.read_csv('tmdb_5000_movies.csv')
credits = pd.read_csv('tmdb_5000_credits.csv') 

movies.head()

credits.head()

movies = movies.merge(credits,on='title')

movies.head(1)

movies = movies[['movie_id','title','overview','genres','keywords','cast','crew']]

import ast

credits.head(1)['cast'].values

movies.head(1)

movies=movies.merge(credits,on='title')

movies.head(1)

credits.head(1)



movies=movies.merge(credits,on='title')

movies.head(1)

movies = movies[['movie_id','title','overview','genres','keywords','cast','crew']]

movies.isnull().sum()

movies.dropna(inplace=True)
movies.isnull().sum()

movies.duplicated().sum()

movies.iloc[0].genres


def convert(obj):
    L = []
    for i in ast.literal_eval(obj):
        L.append(i['name']) 
    return L


movies['genres'] = movies['genres'].apply(convert)

movies.head()

movies['keywords'] = movies['keywords'].apply(convert)

movies.head()


def convert3(obj):
    L = []
    counter = 0
    for i in ast.literal_eval(obj):
        if counter!=3:
            L.append(i['name'])
            counter+=1
        else:
            break
    return L 


movies['cast'] = movies['cast'].apply(convert3)

movies.head()


def fetch_director(obj):
    L = []
    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'][10]

movies['overview'] = movies['overview'].apply(lambda x:x.split())


def collapse(L):
    L1 = []
    for i in L:
        L1.append(i.replace(" ",""))
    return L1


movies['cast'] = movies['cast'].apply(collapse)
movies['crew'] = movies['crew'].apply(collapse)

movies['genres'] = movies['genres'].apply(collapse)
movies['keywords'] = movies['keywords'].apply(collapse)

movies.head()

movies['tags'] = movies['overview'] + movies['genres'] + movies['keywords'] + movies['cast'] + movies['crew']

movies.head()

new = movies.drop(columns=['overview','genres','keywords','cast','crew'])

new.head()

new['tags'] = new['tags'].apply(lambda x: " ".join(x))
new.head()

new['tags'] = new['tags'].apply(lambda x:x.lower())

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)   


new['tags']=new['tags'].apply(stem)

from sklearn.feature_extraction.text import CountVectorizer

cv = CountVectorizer(max_features=5000,stop_words='english')

vector = cv.fit_transform(new['tags']).toarray()

new.head()

 from sklearn.metrics.pairwise import cosine_similarity

similarity = cosine_similarity(vector)

similarity[1]


def recommend(movie):
    index = new[new['title'] == movie].index[0]
    distances = sorted(list(enumerate(similarity[index])),reverse=True,key = lambda x: x[1])
    for i in distances[1:6]:
        print(new.iloc[i[0]].title)


recommend('Gandhi')

import pickle

pickle.dump(new,open('movie_list.pkl','wb'))
pickle.dump(similarity,open('similarity.pkl','wb'))

pickle.dump(new.to_dict(),open('movie_dict.pkl','wb'))



StarcoderdataJupyterScriptsDedupFiltered