text_plain_output_1.png
from sklearn.feature_extraction.text import CountVectorizer

from sklearn.metrics.pairwise import cosine_similarity

import ast

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('/kaggle/input/tmdb-movie-metadata/tmdb_5000_movies.csv')
credit = pd.read_csv('/kaggle/input/tmdb-movie-metadata/tmdb_5000_credits.csv')

movies = movies[['id', 'title', 'overview', 'tagline', 'genres', 'keywords']]

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

movies.drop(['movie_id'], axis=1, inplace=True)

def convert(text):
    L = []
    for i in ast.literal_eval(text):
        L.append(i['name'])
    return L
def convert2(text):
    a = []
    count = 0
    for i in ast.literal_eval(text):
        if count < 3:
            a.append(i['name'])
        count += 1
    return a
def convert3(data):
    a = []
    for i in ast.literal_eval(data):
        if i['job'] == 'Director':
            a.append(i['name'])
    return a

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

def remove_space(data):
    a = []
    for i in data:
        a.append(i.replace(' ', ''))
    return a

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

movies.drop(['overview', 'tagline', 'genres', 'keywords', 'cast', 'crew'], axis=1, inplace=True)

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

vector = cv.fit_transform(movies['tag']).toarray()

from sklearn.metrics.pairwise import cosine_similarity
cosim = cosine_similarity(vector)

def recommend(name):
    index = np.where(movies['title'] == name)[0][0]
    similar_items = sorted(list(enumerate(cosim[index])), key=lambda x: x[1], reverse=True)[0:5]
    data = []
    for i in similar_items:
        v = movies[movies.index == i[0]]['title']
        v = list(v)
        data.append(v)
    return pd.DataFrame(data)

recommend('Avatar')