|
import numpy as np |
|
import pandas as pd |
|
|
|
class ContentBasedRecommender: |
|
def __init__(self, train_data): |
|
self.train_data = train_data |
|
|
|
def predict(self, user_id, k=10): |
|
user_books = set(self.train_data[self.train_data['user_id'] == user_id]['book_id']) |
|
similar_books = set().union(*(self.train_data[self.train_data['book_id'] == book_id]['similar_books'].iloc[0] for book_id in user_books)) |
|
recommended_books = list(similar_books - user_books) |
|
|
|
return np.random.choice(recommended_books, size=min(k, len(recommended_books)), replace=False).tolist() |
|
|