<jupyter_start><jupyter_text>Movie Recommendation System<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.sparse import csr_matrix
from sklearn.neighbors import NearestNeighbors
plt.style.use('dark_background')
import warnings
warnings.filterwarnings(action = 'ignore')
movies = pd.read_csv('movies.csv')
ratings = pd.read_csv('ratings.csv')
ratings.head(15) 
data = ratings.pivot_table(index = "movieId",columns = "userId",values = "rating")
data.head()
data.fillna(0,inplace=True)
data.head()<jupyter_output><empty_output><jupyter_text>Removing noise from data     Qualifying an user - more than 50 movies rated    Qualifying movie - more than 10 users rated<jupyter_code>n_users = ratings.groupby('movieId')['rating'].agg('count')
n_movie = ratings.groupby('userId')['rating'].agg('count')
fig = plt.figure(figsize=(16,4))
plt.scatter(n_users.index,n_users,color = 'red')
plt.axhline(y = 10, color ='green')
plt.xlabel('Movie ID')
plt.ylabel('No of users')
plt.title('movieId v/s no of users')
data = data.loc[n_users[n_users>10].index,:]
fig = plt.figure(figsize=(16,4))
plt.scatter(n_movie.index,n_movie,color = 'red')
plt.axhline(y = 50, color ='green')
plt.xlabel('Users')
plt.ylabel('No of movies')
plt.title('No of movies v/s Users')
data = data.loc[:,n_movie[n_movie>50].index]
data
csr_data = csr_matrix(data.values)
data.reset_index(inplace = True)
knn = NearestNeighbors(metric = 'cosine', n_neighbors = 20, algorithm = 'brute', n_jobs =1).fit(csr_data)
def get_movie_recommendation(movie_name):
    n_movies_to_reccomend = 10
    movie_list = movies[movies['title'].str.contains(movie_name)]  #checking if movie is present in list
    if len(movie_list):        
        movie_idx= movie_list.iloc[0]['movieId'] #index of movie in the 'movie_list' dataframe
        movie_idx = data[data['movieId'] == movie_idx].index[0] #index of movie in modified dataframe 'data'
        #print(movie_idx)
        distances , indices = knn.kneighbors(csr_data[movie_idx],n_neighbors=n_movies_to_reccomend+1) #using knn to find nearest neighbors
        rec_movie_indices = sorted(list(zip(indices.squeeze().tolist(),distances.squeeze().tolist())),key=lambda x: x[1])[:0:-1] 
        #sorted zipping
        #slicing [start:end:step]
        #print(rec_movie_indices)
        recommend_frame = []
        for val in rec_movie_indices:
            movie_idx = data.iloc[val[0]]['movieId'] #takes index of current movie recommended and finds movieId in dataframe 'data'
            #print(movie_idx)
            idx = movies[movies['movieId'] == movie_idx].index #finds index of current movie in movies dataframe using previously found movieId in 'data' dataframe
            #print(movies.iloc[idx]['title'].values[0])
            recommend_frame.append({'Title':movies.iloc[idx]['title'].values[0],'Distance':val[1]})#constructing dataframe
        df = pd.DataFrame(recommend_frame,index=range(1,n_movies_to_reccomend+1))
        return df
    else:
        return "No movies found. Please check your input"
get_movie_recommendation('Iron Man')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13426662424435254, 'fraction_numerical': 0.012090359529112313, 'mean_word_length': 4.977186311787072, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '578326', 'license': 'BSD-4-Clause-UC', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1083, 'n_words': 227, 'path': 'Movie recommendation.ipynb', 'repo_name': 'ZenJen-beep/movierecommend', 'size': '3143'}