Upload model.py
Browse files
model.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
class ContentBasedRecommender:
|
5 |
+
def __init__(self, train_data):
|
6 |
+
self.train_data = train_data
|
7 |
+
|
8 |
+
def predict(self, user_id, k=10):
|
9 |
+
user_books = set(self.train_data[self.train_data['user_id'] == user_id]['book_id'])
|
10 |
+
similar_books = set().union(*(self.train_data[self.train_data['book_id'] == book_id]['similar_books'].iloc[0] for book_id in user_books))
|
11 |
+
recommended_books = list(similar_books - user_books)
|
12 |
+
|
13 |
+
return np.random.choice(recommended_books, size=min(k, len(recommended_books)), replace=False).tolist()
|