<jupyter_start><jupyter_text>Load Library And Data<jupyter_code># importing the library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# to know the ecoding type
import chardet
with open('E:\\Recommendation System\\book.csv', 'rb') as rawdata:
    result = chardet.detect(rawdata.read(100000))
result<jupyter_output><empty_output><jupyter_text>- The encoding standard used in the input file is ISO-8859-1- Hence, to minimize the error while loading the input data, we are passing this encoding standard<jupyter_code># load the dataset 1
books_data = pd.read_csv('E:\\1_ExcelR_data\\0_assignmentsData\\10_Recommendation System\\book.csv', encoding='ISO-8859-1')
books_data<jupyter_output><empty_output><jupyter_text>Data Cleaning And EDA<jupyter_code># drop unnecessary column
books_data.drop(['Unnamed: 0'], axis = 1, inplace=True)
books_data.head()
books_data.sort_values(by=['User.ID'])
# data dimenssion
books_data.shape
# data description
books_data.describe().T
# dataframes types
books_data.dtypes
# informartion of the data
books_data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 3 columns):
User.ID        10000 non-null int64
Book.Title     10000 non-null object
Book.Rating    10000 non-null int64
dtypes: int64(2), object(1)
memory usage: 234.5+ KB<jupyter_text>- No null values- two features are numeric- one feature is categorical<jupyter_code>books_data.describe()['Book.Rating']<jupyter_output><empty_output><jupyter_text>- max rating = 10- min rating = 1- average rating = 7.5<jupyter_code># find the minimum and maximum ratings
print('Minimum rating is:', (books_data['Book.Rating'].min()))
print('Maximum rating is:', (books_data['Book.Rating'].max()))<jupyter_output>Minimum rating is: 1
Maximum rating is: 10<jupyter_text>- Most of the books are getting max ratings as 8- Minimum ratings as 1 are very few books<jupyter_code># Unique Users and ratings
print("Total data \n")
print("Total no of ratings   :",books_data.shape[0])
print("Total No of Users     :", len(np.unique(books_data['User.ID'])))
print("Total No of products  :", len(np.unique(books_data['Book.Rating'])))
# find out the average rating for each and every books
Average_ratings = pd.DataFrame(books_data.groupby('Book.Title')['Book.Rating'].mean())
Average_ratings.head(3)<jupyter_output><empty_output><jupyter_text>- Average ratings received by readers as,1) 8.0 - Jason, Madison &amp2) 6.0 - Other Stories;Merril;1985;McClelland &amp3) 4.0 - Repairing PC Drives &amp	  Visualize The Data<jupyter_code># Check the distribution of the rating
plt.figure(figsize=(10, 5))
sns.countplot("Book.Rating", data = books_data)
plt.title('Rating distrubutions', fontsize = 20)
plt.xlabel("Book ratings", fontsize = 15)
plt.ylabel("Total counts", fontsize = 15)
plt.show()<jupyter_output><empty_output><jupyter_text>Building The Recommender<jupyter_code># make pivot table
book_users = books_data.pivot_table( index='User.ID', columns = books_data['Book.Title'], values='Book.Rating')
book_users
# find correlation between "10 Commandments Of Dating" and other books
book_read = book_users["10 Commandments Of Dating"]
similarity_with_other_books = book_users.corrwith(book_read)  
similarity_with_other_books = similarity_with_other_books.sort_values(ascending=False)
similarity_with_other_books.head(10)
# imputer NaN with 0
book_users.fillna(0, inplace = True)
book_users
# collecting unique user id
book_users.index = books_data['User.ID'].unique()
book_users.head()<jupyter_output><empty_output><jupyter_text>Computation with Cosine Distance<jupyter_code># calculating Cosine Similarities between Users
from sklearn.metrics import pairwise_distances
from scipy.spatial.distance import cosine, correlation
# Cosine similarities values (using distance matrics)
user_sim = 1 - pairwise_distances(book_users.values, metric = 'cosine')
user_sim
# store the result (Cosine Similarities values) in a dataframe
user_similarity_df = pd.DataFrame(user_sim)
user_similarity_df
# set the index and columns to userId
user_similarity_df.index = books_data['User.ID'].unique()
user_similarity_df.columns = books_data['User.ID'].unique()
books_data
user_similarity_df.iloc[0:7, 0:7]
np.fill_diagonal(user_sim, 0)
user_similarity_df.iloc[0:7, 0:7]<jupyter_output><empty_output><jupyter_text>Most Similarity<jupyter_code># Most similar readers
user_similarity_df.idxmax(axis = 1)[0:20]
# find out book read by two users 276780 and 276726
books_data[(books_data['User.ID'] == 276780) | (books_data['User.ID'] == 276726)]
# user 276780 books
user_276780 = books_data[books_data['User.ID'] == 276780]
user_276780
# user 276726 books
user_276726 = books_data[books_data['User.ID'] == 276726]
user_276726<jupyter_output><empty_output><jupyter_text>Recommendations<jupyter_code># meging two user book data into single one
pd.merge(user_276780, user_276726, on = 'Book.Title', how = 'outer')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11486892135281168, 'fraction_numerical': 0.040224134480688414, 'mean_word_length': 6.971291866028708, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '8342', 'license': 'MIT', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1673, 'n_words': 426, 'path': '10_Recommendation System/BookRecomSolution.ipynb', 'repo_name': 'kunalk3/eR_task', 'size': '4997'}