<jupyter_start><jupyter_text>Importing the library pandas for data readiing and data manipulation<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Loading and Reading the data<jupyter_code>movies = pd.read_csv("M:\\ml-latest-small\\ml-latest-small\\movies.csv")
ratings = pd.read_csv("M:\\ml-latest-small\\ml-latest-small\\ratings.csv")<jupyter_output><empty_output><jupyter_text>Reading few rows and columns<jupyter_code>movies.head()
ratings.head()<jupyter_output><empty_output><jupyter_text>Taking the total no of rows and features<jupyter_code>movies.shape
ratings.shape
movies.columns
ratings.columns<jupyter_output><empty_output><jupyter_text>Checking if there are any null values or not<jupyter_code>movies.isnull().any()
ratings.isnull().any()<jupyter_output><empty_output><jupyter_text>Converting movies into a Dataframe and using only two columns i.e. movieId and title<jupyter_code>df_movies = pd.read_csv("M:\\ml-latest-small\\ml-latest-small\\movies.csv"\
                        , usecols = ['movieId', 'title'],\
                        dtype = {'movieId':'int32', 'title':'str'})
df_movies.head()
df_movies.describe()<jupyter_output><empty_output><jupyter_text>Similarly for ratings<jupyter_code>df_ratings = pd.read_csv("M:\\ml-latest-small\\ml-latest-small\\ratings.csv"\
                        , usecols = ['userId','movieId', 'rating'],\
                        dtype = {'userId':'int32','movieId':'int32', 'rating':'float32'})
df_ratings.head()
df_ratings.describe()<jupyter_output><empty_output><jupyter_text>Creating a sparse matrix<jupyter_code>from scipy.sparse import csr_matrix
user_movies = df_ratings.pivot(index = 'movieId' , columns = 'userId' , values = 'rating' ) 
user_movies<jupyter_output><empty_output><jupyter_text>Fill all the null values with 0 in that sparse matrix<jupyter_code>user_movies = df_ratings.pivot(index = 'movieId' , columns = 'userId' , values = 'rating' ).fillna(0)
user_movies
mat_movies_csr = csr_matrix(user_movies.values)
mat_movies_csr<jupyter_output><empty_output><jupyter_text>Importing the library NearestNeighbors from sklearn.neighbors<jupyter_code>from sklearn.neighbors import NearestNeighbors
model_knn = NearestNeighbors(metric = 'cosine', algorithm = 'brute' , n_neighbors = 24 )
model_knn.fit(mat_movies_csr)<jupyter_output><empty_output><jupyter_text>Importing process library from fuzzywuzzy<jupyter_code>from fuzzywuzzy import process<jupyter_output><empty_output><jupyter_text>Creating a function for movie recommendation<jupyter_code>def recommender(movie_name, data,model, n_recommendations ):
    model.fit(data)
    idx=process.extractOne(movie_name, df_movies['title'])[2]
    print('Movie Selected: ',df_movies['title'][idx], 'Index: ',idx)
    print('Searching for recommendations.....')
    distances, indices=model.kneighbors(data[idx], n_neighbors=n_recommendations)
    for i in indices:
        print(df_movies['title'][i].where(i!=idx))
    
recommender('Speed', mat_movies_csr, model_knn,20)<jupyter_output>Movie Selected:  Speed (1994) Index:  334
Searching for recommendations.....
334                                     NaN
337                        True Lies (1994)
418                    Jurassic Park (1993)
398                    Fugitive, The (1993)
138       Die Hard: With a Vengeance (1995)
507       Terminator 2: Judgment Day (1991)
436                   Mrs. Doubtfire (1993)
509                           Batman (1989)
314                     <NAME> (1994)
514                     Pretty Woman (1990)
9                          GoldenEye (1995)
506                          Aladdin (1992)
97                        Braveheart (1995)
123                        Apollo 13 (1995)
472             Sleepless in Seattle (1993)
546              Mission: Impossible (1996)
322                   Lion King, The (1994)
325                        Mask, The (1994)
43              Seven (a.k.a. Se7en) (1995)
615    Independence Day (a.k.a. ID4) (1996)
Name: title, dtype: object
{'fraction_non_alphanumeric': 0.11619190404797601, 'fraction_numerical': 0.03973013493253373, 'mean_word_length': 3.4726256983240225, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '99702', 'license': 'MIT', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1310, 'n_words': 260, 'path': 'Recommendation_System_KNN.ipynb', 'repo_name': 'amber5634/Movie-Recommendation-using-KNN', 'size': '4008'}