<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
ratings_df = pd.read_csv("/content/drive/MyDrive/DATA SCIENCE/Datasets/ratings_small.csv")
ratings_df.shape
ratings_df.head()
movies_df = pd.read_csv("/content/drive/MyDrive/DATA SCIENCE/Datasets/movies_metadata.csv")
ratings_df.head()
movies_df.head()
movies_df.drop(movies_df.index[19730],inplace=True)
movies_df.drop(movies_df.index[29502],inplace=True)
movies_df.drop(movies_df.index[35585],inplace=True)
movies_df.id = movies_df.id.astype(np.int64)
type(movies_df.id[0])
ratings_df.movieId.isin(movies_df.id).sum()
ratings_df = pd.merge(ratings_df,movies_df[['title','id']],left_on='movieId',right_on='id')
ratings_df.head()
ratings_df.drop(['timestamp','id'],axis=1,inplace=True)
ratings_df.shape
ratings_df.sample(5)
ratings_df.isnull().sum()
ratings_count = ratings_df.groupby(by="title")['rating'].count().reset_index().rename(columns={'rating':'totalRatings'})[['title','totalRatings']]
ratings_count.shape[0]
len(ratings_df['title'].unique())
ratings_count.sample(5)
ratings_count.head()
ratings_df.head()
ratings_total= pd.merge(ratings_df, ratings_count, on = 'title', how = 'left')
ratings_total.shape
ratings_total.head()
ratings_count['totalRatings'].describe()
ratings_count['totalRatings'].quantile(np.arange(.6,1,0.01))
votes_count_threshold = 20
ratings_top = ratings_total.query('totalRatings > @votes_count_threshold')
ratings_top.shape
ratings_top.head()
if not ratings_top[ratings_top.duplicated(['userId','title'])].empty:
    ratings_top = ratings_top.drop_duplicates(['userId','title'])
ratings_top.shape
df_for_knn = ratings_top.pivot(index='title',columns='userId',values='rating').fillna(0)
df_for_knn.head()
df_for_knn.shape
from scipy.sparse import csr_matrix
df_for_knn_sparse = csr_matrix(df_for_knn.values)
from sklearn.neighbors import NearestNeighbors
model_knn = NearestNeighbors(metric='cosine',algorithm='brute')
model_knn.fit(df_for_knn_sparse)
query_index = np.random.choice(df_for_knn.shape[0])
distances, indices = model_knn.kneighbors(df_for_knn.loc['Batman Returns'].values.reshape(1,-1),n_neighbors=6)
for i in range(0,len(distances.flatten())):
    if i==0:
        print("Recommendations for movie: {0}\n".format(df_for_knn.index[query_index]))
    else:
        print("{0}: {1}, with distance of {2}".format(i,df_for_knn.index[indices.flatten()[i]],distances.flatten()[i]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16344969199178644, 'fraction_numerical': 0.01642710472279261, 'mean_word_length': 9.278481012658228, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '644760', 'license': 'CC0-1.0', 'n_tokens_mistral': 983, 'n_tokens_neox': 954, 'n_words': 103, 'path': 'Movie_Recommendation_Engine_Using_KNN_.ipynb', 'repo_name': 'djswoosh/Uni-Bits', 'size': '2435'}