image_output_1.png
from sklearn.cluster import MiniBatchKMeans

from sklearn.feature_extraction.text import TfidfVectorizer

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)
data['cluster'].value_counts().head()