Book-Recommendation-System-using-Python
Before we delve into the code, let’s briefly understand the data we’ll be working with:
Below are the key steps in our Python implementation for building a book recommendation system using collaborative filtering:
Here’s a detailed Python code example that demonstrates the implementation of collaborative filtering for book recommendations:
import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
# Load datasets
books = pd.read_csv('books.csv', encoding='latin')
ratings = pd.read_csv('ratings.csv', encoding='latin')
users = pd.read_csv('users.csv', encoding='latin')
# Data preprocessing
merged_data = ratings.merge(books, on='isbn')
user_counts = merged_data.groupby('user_id').size()
book_counts = merged_data.groupby('book_title').size()
good_users = user_counts[user_counts > 200].index
good_books = book_counts[book_counts > 50].index
filtered_data = merged_data[(merged_data['user_id'].isin(good_users)) & (merged_data['book_title'].isin(good_books))]
# User-based Collaborative Filtering
pivot_user = filtered_data.pivot_table(index='book_title', columns='user_id', values='rating').fillna(0)
user_similarity = cosine_similarity(pivot_user)
def recommend_user(book_title):
book_index = pivot_user.index.get_loc(book_title)
similar_books = sorted(list(enumerate(user_similarity[book_index])), key=lambda x: x[1], reverse=True)[1:6]
recommended_books = [pivot_user.index[i[0]] for i in similar_books]
return recommended_books
print("User-Based Recommendations for '1984':", recommend_user("1984"))
# Item-based Collaborative Filtering
pivot_item = filtered_data.pivot_table(index='user_id', columns='book_title', values='rating').fillna(0)
item_similarity = cosine_similarity(pivot_item.T)
def recommend_item(book_title):
book_index = pivot_item.columns.get_loc(book_title)
similar_books = sorted(list(enumerate(item_similarity[book_index])), key=lambda x: x[1], reverse=True)[1:6]
recommended_books = [pivot_item.columns[i[0]] for i in similar_books]
return recommended_books
print("Item-Based Recommendations for '1984':", recommend_item("1984"))
Book recommendation systems play a crucial role in enhancing user experience and engagement on online platforms. Collaborative filtering, whether user-based or item-based, is a powerful technique for generating personalized recommendations based on user behavior and preferences. By leveraging techniques like cosine similarity and data preprocessing, we can build effective recommendation systems that help users discover new and relevant content tailored to their interests. Whether you’re a data scientist, developer, or simply curious about recommendation systems, exploring and implementing these algorithms can offer valuable insights into the world of personalized content recommendations.
GitHub DataSet And Python Code of this Project : Click Here
TL;DR Summary Best Hill Station: Mount Abu is the only hill station in Rajasthan, perfect…
Why Indian Students Need AI Tools Right Now Let's be real - being a student…
India Known for their graciousness and hospitality, Indians believe in the Sanskrit phrase Atithi Devo…
Ready to ditch the Delhi traffic and dive into a refreshing adventure? Whether you're a…
n today’s globalized world, working across international borders has become a common practice. For Indian…
Ever wonder how Netflix knows exactly what show you’ll love next? Or how Amazon suggests…
This website uses cookies.