# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import pandas as pd

# # Reading Datasets

# Reading dummy books dataset

books = pd.read_csv('/home/student/Desktop/archive/BX-Books.csv',sep=';', encoding="latin-1", error_bad_lines=False)


# Reading dummy users dataset

users = pd.read_csv("/home/student/Desktop/archive/BX-Users.csv", sep=';', encoding="latin-1", error_bad_lines=False)


# Reading ratings dataset
#
# Since our main theme is not to recommend books based on ratings, but based on search. Since we dont have search dataset, we are simulating the idea of book recommendation based on ratings(because of available datasets)

ratings = pd.read_csv("/home/student/Desktop/archive/BX-Book-Ratings.csv", sep=';', encoding="latin-1", error_bad_lines=False)



books.count()

# # Preprocessing the data

books = books[['ISBN', 'Book-Title', 'Book-Author', 'Year-Of-Publication', 'Publisher']]

books.head()

books.rename(columns = {'Book-Title':'title', 'Book-Author':'author', 'Year-Of-Publication':'year', 'Publisher':'publisher'}, inplace=True)


users.rename(columns = {'User-ID':'user_id', 'Location':'location', 'Age':'age'}, inplace=True)
ratings.rename(columns = {'User-ID':'user_id', 'Book-Rating':'rating'}, inplace=True)

users.head()

books.head()

ratings.head()

ratings['user_id'].value_counts()

users_rated_200_books = ratings['user_id'].value_counts()>200

x = ratings['user_id'].value_counts() > 200
y = x[x].index  #user_ids
print(y.shape)
ratings = ratings[ratings['user_id'].isin(y)]

rating_with_books = ratings.merge(books, on='ISBN')
rating_with_books.head()

number_rating = rating_with_books.groupby('title')['rating'].count().reset_index()
number_rating.rename(columns= {'rating':'number_of_ratings'}, inplace=True)
final_rating = rating_with_books.merge(number_rating, on='title')
final_rating.shape
final_rating = final_rating[final_rating['number_of_ratings'] >= 50]
final_rating.drop_duplicates(['user_id','title'], inplace=True)

book_pivot = final_rating.pivot_table(columns='user_id', index='title', values="rating")
book_pivot.fillna(0, inplace=True)

from scipy.sparse import csr_matrix
book_sparse = csr_matrix(book_pivot)

from sklearn.neighbors import NearestNeighbors
model = NearestNeighbors(algorithm='brute')
model.fit(book_sparse)

distances, suggestions = model.kneighbors(book_pivot.iloc[500, :].values.reshape(1, -1))

# # Lets see what are the recommendations based on his ratings count

for i in range(len(suggestions)):
  print(book_pivot.index[suggestions[i]])

distances



StarcoderdataJupyterScriptsDedupFiltered