Exploring Book Recommendation Systems: Collaborative Filtering Approach- Vikash Goyal

Book Recommendation Systems

Understanding the Data

Before we delve into the code, let’s briefly understand the data we’ll be working with:

  • Books Data: Contains information about books such as ISBN, title, author, publication year, and publisher.
  • Rating Data: Includes user ratings for various books identified by ISBN.
  • User Data: Provides details about users such as location and age.

Python Code Overview

Below are the key steps in our Python implementation for building a book recommendation system using collaborative filtering:

  1. Importing necessary libraries and loading data.
  2. Data preprocessing to filter out users and books with insufficient data.
  3. Implementing user-based collaborative filtering.
  4. Implementing item-based collaborative filtering.

Flowchart Overview

Load Data Preprocessing User-Based CF

Python Code Example

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"))
  

Conclusion

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

Leave a comment