<jupyter_start><jupyter_text>This is an implementation of Content Based Recommendation system on Netflix Movie Dataset  Import Libraries<jupyter_code># Import Libraries
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
import nltk
import string
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from string import punctuation
from wordcloud import WordCloud<jupyter_output><empty_output><jupyter_text>Import Dataset<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
df_netflix = pd.read_csv("drive/My Drive/datasets/netflix_titles.csv")
print (df_netflix.shape)
df_netflix.head()<jupyter_output>(7787, 12)<jupyter_text>Graphical Analysis<jupyter_code>plt.figure(figsize=(10,10))
sns.set_style("dark")
sns.countplot(x ='type', data = df_netflix)
plt.plot()
#for countries in df_netflix["country"]:
print("Any null values in country column : ",df_netflix["country"].isnull().values.any())

list_country = list()
for countries in df_netflix["country"].dropna():
  for country in countries.split(", "):
    list_country.append(country)

from collections import Counter
df_netflix_country_count = pd.DataFrame(Counter(list_country).most_common()[:10], columns=['Country', 'Count'])
plt.figure(figsize=(10,10))
sns.set(style="darkgrid")
ax = sns.barplot(y="Country", x='Count', data=df_netflix_country_count)
plt.figure(figsize=(10,10))
sns.set(style="darkgrid")
ax = sns.countplot(y='release_year', data=df_netflix)
plt.figure(figsize=(12,10))
sns.set(style="darkgrid")
ax = sns.countplot(y='release_year', data=df_netflix, order=df_netflix['release_year'].value_counts().index[0:15])<jupyter_output><empty_output><jupyter_text>Data preprocessing<jupyter_code>df_netflix.drop(columns=['director', 'cast', 'country', 'date_added', 'release_year', 'rating', 'duration', 'type'], inplace=True)
df_netflix.head()
# https://stackoverflow.com/questions/42750551/converting-strings-to-a-lower-case-in-pandas
df_netflix['title_list'] = df_netflix['title'].str.lower()
df_netflix.listed_in = df_netflix.listed_in.astype(str).str.lower()
df_netflix.description = df_netflix.description.astype(str).str.lower()
nltk.download('punkt')
df_netflix['title_list'] = df_netflix['title_list'].apply(word_tokenize)
df_netflix['listed_in'] = df_netflix['listed_in'].apply(word_tokenize)
df_netflix['description'] = df_netflix['description'].apply(word_tokenize)
df_netflix
# https://stackoverflow.com/questions/29523254/python-remove-stop-words-from-pandas-dataframe
nltk.download('stopwords')
list_stopwords = set(stopwords.words('english') + list(punctuation))
df_netflix['title_list'] = df_netflix['title_list'].apply(lambda x: [word for word in x if word not in list_stopwords])
df_netflix['listed_in'] = df_netflix['listed_in'].apply(lambda x: [word for word in x if word not in list_stopwords])
df_netflix['description'] = df_netflix['description'].apply(lambda x: [word for word in x if word not in list_stopwords])
df_netflix['description'] = df_netflix['description'].apply(lambda x : [word.translate(str.maketrans('', '', string.punctuation)) for word in x])
df_netflix['description'] = df_netflix['description'].apply(lambda x : [word for word in x if len(word) > 0])
df_netflix['title_list'] = df_netflix['title_list'].apply(lambda x : list(set(x)))
df_netflix['listed_in'] = df_netflix['listed_in'].apply(lambda x : list(set(x)))
df_netflix['description'] = df_netflix['description'].apply(lambda x : list(set(x)))
df_netflix<jupyter_output><empty_output><jupyter_text>Wordcloud visualization<jupyter_code>list_genre = list()
for genres in df_netflix['listed_in']:
  for genre in genres:
    list_genre.append(genre)
genre = ' '.join(list_genre)

plt.figure(figsize=(16,12))
wordcloud = WordCloud(max_font_size=50, max_words=100,background_color="white").generate(genre)
plt.imshow(wordcloud,interpolation="bilinear")
plt.axis("off")
plt.show()
list_description = list()
for descriptions in df_netflix['description']:
  for description	in descriptions:
    list_description.append(description)
description	= ' '.join(list_description)

plt.figure(figsize=(16,12))
wordcloud = WordCloud(max_font_size=50, max_words=100,background_color="white").generate(description)
plt.imshow(wordcloud,interpolation="bilinear")
plt.axis("off")
plt.show()<jupyter_output><empty_output><jupyter_text>Gensim is an open source Python library for natural language processing, with a focus on topic modeling.
Word2vec is one algorithm for learning a word embedding from a text corpus.  Pretrained Google's Word2Vec Model<jupyter_code>import gensim
!wget -c "https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz"
!gunzip GoogleNews-vectors-negative300.bin.gz
# https://machinelearningmastery.com/develop-word-embeddings-python-gensim/
model = gensim.models.KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin", binary=True)
len(model.vocab)
matrix_netflix_vocab = []
for list_ in df_netflix.to_numpy():
    list_[2] = [word for word in list_[2] if word in model.vocab]
    list_[3] = [word for word in list_[3] if word in model.vocab]
    list_[4] = [word for word in list_[4] if word in model.vocab]
    matrix_netflix_vocab.append(list_)
df_netflix_vocab = pd.DataFrame(matrix_netflix_vocab, columns=df_netflix.columns)
df_netflix_vocab<jupyter_output><empty_output><jupyter_text>Recommedation based on title,genre and description<jupyter_code>from tqdm import tqdm

def recommendation(title):
    matrix_netflix_title_vocab = []
    for list_ in df_netflix[df_netflix['title'] == title].to_numpy():
        list_[2] = [word for word in list_[2] if word in model.vocab]
        list_[3] = [word for word in list_[3] if word in model.vocab]
        list_[4] = [word for word in list_[4] if word in model.vocab]
        matrix_netflix_title_vocab.append(list_)

    matrix_similarity = []
    pbar = tqdm(matrix_netflix_vocab)

    for list1 in pbar:
        for list2 in matrix_netflix_title_vocab:
            score_catg = model.n_similarity(list1[2], list2[2])
            score_desc = model.n_similarity(list1[3], list2[3])
            try:
                score_title = model.n_similarity(list1[4], list2[4])/2
            except:
                score_title = 0
            if ((list1[1] != list2[1]) & (score_catg > 0.85)):
                matrix_similarity.append([list1[1], list2[1], score_title, score_catg, score_desc])
        pbar.update()
    pbar.close()
    df_netflix_similarity = pd.DataFrame(matrix_similarity, columns = ['recommendation','title','score_title', 'score_category', 'score_description'])
    df_netflix_similarity['final_score'] = df_netflix_similarity['score_title'] + df_netflix_similarity['score_category'] + df_netflix_similarity['score_description']
    return (df_netflix_similarity.sort_values(by=['final_score', 'score_category', 'score_description', 'score_title'], ascending=False).head(10))
# Practice
# https://tedboy.github.io/nlps/generated/generated/gensim.models.Word2Vec.n_similarity.html
print(model.n_similarity(['cat','dog'],['dog']))
print(model.n_similarity(['sushi', 'shop'], ['japanese', 'restaurant']))
print(model.n_similarity(['restaurant', 'japanese'], ['japanese', 'restaurant']))<jupyter_output>0.9371329
0.5983723
1.0<jupyter_text>Sample recommedations<jupyter_code>recommendation('Avengers: Infinity War')
recommendation('Transformers: Robots in Disguise')
recommendation('Friends')
recommendation("Article 15")<jupyter_output>100%|██████████| 7787/7787 [00:02<00:00, 3278.35it/s]
{'fraction_non_alphanumeric': 0.137949052906597, 'fraction_numerical': 0.021293272370999348, 'mean_word_length': 6.1551401869158875, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '424762', 'license': 'MIT', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2667, 'n_words': 470, 'path': 'Content-Based-Movie-Recommendation/Content_Based_Netflix_Movie_Recommendation_System.ipynb', 'repo_name': 'prakhargurawa/Movie-Recommendation-System-Netflix-Dataset', 'size': '7655'}