text_plain_output_1.png
from sklearn.cluster import MiniBatchKMeans

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.metrics.pairwise import linear_kernel

import matplotlib.pyplot as plt

import networkx as nx

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

data = pd.read_csv('/kaggle/input/netflix-shows/netflix_titles.csv')

data['date_added'] = pd.to_datetime(data['date_added'])
data['year'] = data['date_added'].dt.year
data['month'] = data['date_added'].dt.month
data['day'] = data['date_added'].dt.day

data['directors'] = data['director'].apply(lambda x: [] if pd.isna(x) else [i.strip() for i in x.split(',')])
data['actors'] = data['cast'].apply(lambda x: [] if pd.isna(x) else [i.strip() for i in x.split(',')])
data['categories'] = data['listed_in'].apply(lambda x: [] if pd.isna(x) else [i.strip() for i in x.split(',')])
data['countries'] = data['country'].apply(lambda x: [] if pd.isna(x) else [i.strip() for i in x.split(',')])

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from sklearn.cluster import MiniBatchKMeans
text_content = data['description']
vector = TfidfVectorizer(max_df=0.3, min_df=1, stop_words='english', lowercase=True, use_idf=True, norm=u'l2', smooth_idf=True)
tfidf = vector.fit_transform(text_content)
kmeans = MiniBatchKMeans(n_clusters=200)
kmeans.fit(tfidf)
centers = kmeans.cluster_centers_.argsort()[:, ::-1]
terms = vector.get_feature_names()
request_transform = vector.transform(data['description'])
data['cluster'] = kmeans.predict(request_transform)

def find_similar(tfidf_matrix, index, top_n=5):
    cosine_similarities = linear_kernel(tfidf_matrix[index:index + 1], tfidf_matrix).flatten()
    related_docs_indices = [i for i in cosine_similarities.argsort()[::-1] if i != index]
    return [index for index in related_docs_indices][0:top_n]

G = nx.Graph(label='NETFLIX')
for i, row in data.iterrows():
    G.add_node(row['title'], key=row['show_id'], label='MOVIE', mtype=row['type'], rating=row['rating'])
    for j in row['actors']:
        G.add_node(j, label='PERSON')
        G.add_edge(row['title'], j, label='ACTED_IN')
    for j in row['directors']:
        G.add_node(j, label='PERSON')
        G.add_edge(row['title'], j, label='DIRECTED')
    for j in row['categories']:
        G.add_node(j, label='CAT')
        G.add_edge(row['title'], j, label='CAT_IN')
    for j in row['countries']:
        G.add_node(j, label='COUNTRY')
        G.add_edge(row['title'], j, label='COUNTRY_IN')
for i, row in data.iterrows():
    similar = find_similar(tfidf, i, top_n=5)
    for e in similar:
        G.add_edge(row['title'], data['title'].loc[e], label='SIMILAR_TO')

G.number_of_nodes()

G.number_of_edges()

def get_all_adj_nodes(list_in):
    sub_graph = set()
    for m in list_in:
        sub_graph.add(m)
        for e in G.neighbors(m):
            sub_graph.add(e)
    return list(sub_graph)
def draw_sub_graph(sub_graph):
    subgraph = G.subgraph(sub_graph)
    colors = []
    for e in subgraph.nodes():
        if G.nodes[e]['label'] == 'MOVIE':
            colors.append('blue')
        elif G.nodes[e]['label'] == 'PERSON':
            colors.append('red')
        elif G.nodes[e]['label'] == 'CAT':
            colors.append('green')
        elif G.nodes[e]['label'] == 'COUNTRY':
            colors.append('yellow')
        elif G.nodes[e]['label'] == 'SIMILAR_TO':
            colors.append('orange')

list_in = ["Ocean's Twelve", "Ocean's Thirteen"]
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = [14, 14]
sub_graph = get_all_adj_nodes(list_in)
draw_sub_graph(sub_graph)