Spaces:
Sleeping
Sleeping
Create recommendation_utils.py
Browse files- src/recommendation_utils.py +65 -0
src/recommendation_utils.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
|
| 6 |
+
# Lade Modelle und Daten
|
| 7 |
+
def load_nn_model(path):
|
| 8 |
+
return load_model(path)
|
| 9 |
+
|
| 10 |
+
def load_svd_model(path):
|
| 11 |
+
with open(path, "rb") as f:
|
| 12 |
+
return pickle.load(f)
|
| 13 |
+
|
| 14 |
+
def load_trainset(path):
|
| 15 |
+
with open(path, "rb") as f:
|
| 16 |
+
return pickle.load(f)
|
| 17 |
+
|
| 18 |
+
def fold_in_new_user(model, trainset, user_ratings, reg=5):
|
| 19 |
+
n_factors = model.n_factors
|
| 20 |
+
A = np.zeros((n_factors, n_factors))
|
| 21 |
+
b = np.zeros(n_factors)
|
| 22 |
+
bias_numerator, bias_denominator = 0, 0
|
| 23 |
+
global_mean = trainset.global_mean
|
| 24 |
+
|
| 25 |
+
for movie_id, rating in user_ratings.items():
|
| 26 |
+
try:
|
| 27 |
+
movie_id = int(movie_id)
|
| 28 |
+
inner_iid = trainset.to_inner_iid(movie_id)
|
| 29 |
+
qi = model.qi[inner_iid]
|
| 30 |
+
bi = model.bi[inner_iid]
|
| 31 |
+
A += np.outer(qi, qi)
|
| 32 |
+
b += qi * (rating - global_mean - bi)
|
| 33 |
+
bias_numerator += (rating - global_mean - bi)
|
| 34 |
+
bias_denominator += 1
|
| 35 |
+
except ValueError:
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
A += reg * np.eye(n_factors)
|
| 39 |
+
new_user_factors = np.linalg.solve(A, b) if np.linalg.det(A) != 0 else np.linalg.pinv(A) @ b
|
| 40 |
+
new_user_bias = bias_numerator / bias_denominator if bias_denominator > 0 else 0
|
| 41 |
+
return new_user_factors, new_user_bias
|
| 42 |
+
|
| 43 |
+
def recommend_with_svd(model, trainset, ratings_df, user_ratings, top_n=10):
|
| 44 |
+
new_user_factors, new_user_bias = fold_in_new_user(model, trainset, user_ratings)
|
| 45 |
+
global_mean = trainset.global_mean
|
| 46 |
+
movie_predictions = []
|
| 47 |
+
for inner_iid in range(trainset.n_items):
|
| 48 |
+
movie_id = trainset.to_raw_iid(inner_iid)
|
| 49 |
+
if movie_id not in user_ratings:
|
| 50 |
+
pred = global_mean + new_user_bias + model.bi[inner_iid] + np.dot(new_user_factors, model.qi[inner_iid])
|
| 51 |
+
movie_predictions.append({'movieId': int(movie_id), 'rating': pred})
|
| 52 |
+
df = pd.DataFrame(movie_predictions).sort_values("rating", ascending=False).head(top_n)
|
| 53 |
+
return df
|
| 54 |
+
|
| 55 |
+
def recommend_with_nn(user_ratings, model, available_movies, top_n=10):
|
| 56 |
+
user_id = max(user_ratings.keys(), default=0) + 100000 # Dummy user ID
|
| 57 |
+
user_vector = np.array([user_id] * len(available_movies))
|
| 58 |
+
movie_vector = np.array(available_movies)
|
| 59 |
+
predictions = model.predict([user_vector, movie_vector], verbose=0)
|
| 60 |
+
df = pd.DataFrame({
|
| 61 |
+
'movieId': available_movies,
|
| 62 |
+
'rating': predictions.flatten()
|
| 63 |
+
})
|
| 64 |
+
df = df[~df["movieId"].isin(user_ratings.keys())]
|
| 65 |
+
return df.sort_values("rating", ascending=False).head(top_n)
|