lenawilli commited on
Commit
268b919
Β·
verified Β·
1 Parent(s): aae2709

Update src/recommendation_utils.py

Browse files
Files changed (1) hide show
  1. src/recommendation_utils.py +29 -4
src/recommendation_utils.py CHANGED
@@ -1,20 +1,23 @@
1
  import numpy as np
2
  import pandas as pd
3
  import pickle
4
- from keras.models import load_model
5
  from keras.models import model_from_json
6
  from keras.optimizers import Adam
7
 
 
 
 
8
  def load_nn_model(config_path, weights_path):
9
  with open(config_path, "r") as f:
10
  model_json = f.read()
11
  model = model_from_json(model_json)
12
  model.load_weights(weights_path)
13
-
14
- # same config as used in training
15
  model.compile(optimizer=Adam(), loss="mse", metrics=["mse", "mae"])
16
  return model
17
 
 
 
 
18
  def load_svd_model(path):
19
  with open(path, "rb") as f:
20
  return pickle.load(f)
@@ -23,10 +26,16 @@ def load_trainset(path):
23
  with open(path, "rb") as f:
24
  return pickle.load(f)
25
 
 
 
 
26
  def load_encodings(path="encodings.pkl"):
27
  with open(path, "rb") as f:
28
  return pickle.load(f)
29
 
 
 
 
30
  def fold_in_new_user(model, trainset, user_ratings, reg=5):
31
  n_factors = model.n_factors
32
  A = np.zeros((n_factors, n_factors))
@@ -64,10 +73,26 @@ def recommend_with_svd(model, trainset, ratings_df, user_ratings, top_n=10):
64
  df = pd.DataFrame(movie_predictions).sort_values("rating", ascending=False).head(top_n)
65
  return df
66
 
 
 
 
67
  def recommend_with_nn(user_ratings, model, available_movies, top_n=10):
68
- user_id = max(user_ratings.keys(), default=0) + 100000 # Dummy user ID
 
 
 
 
 
 
 
 
 
 
 
 
69
  user_vector = np.array([user_id] * len(available_movies))
70
  movie_vector = np.array(available_movies)
 
71
  predictions = model.predict([user_vector, movie_vector], verbose=0)
72
  df = pd.DataFrame({
73
  'movieId': available_movies,
 
1
  import numpy as np
2
  import pandas as pd
3
  import pickle
 
4
  from keras.models import model_from_json
5
  from keras.optimizers import Adam
6
 
7
+ # ─────────────────────────────────────────────────────────────────────────────
8
+ # Neural Network Model Laden (aus JSON + Weights)
9
+ # ─────────────────────────────────────────────────────────────────────────────
10
  def load_nn_model(config_path, weights_path):
11
  with open(config_path, "r") as f:
12
  model_json = f.read()
13
  model = model_from_json(model_json)
14
  model.load_weights(weights_path)
 
 
15
  model.compile(optimizer=Adam(), loss="mse", metrics=["mse", "mae"])
16
  return model
17
 
18
+ # ─────────────────────────────────────────────────────────────────────────────
19
+ # SVD Model & Trainset Laden
20
+ # ─────────────────────────────────────────────────────────────────────────────
21
  def load_svd_model(path):
22
  with open(path, "rb") as f:
23
  return pickle.load(f)
 
26
  with open(path, "rb") as f:
27
  return pickle.load(f)
28
 
29
+ # ─────────────────────────────────────────────────────────────────────────────
30
+ # Encodings (dict mit user/movie Encodings) laden
31
+ # ─────────────────────────────────────────────────────────────────────────────
32
  def load_encodings(path="encodings.pkl"):
33
  with open(path, "rb") as f:
34
  return pickle.load(f)
35
 
36
+ # ─────────────────────────────────────────────────────────────────────────────
37
+ # SVD Recommendation
38
+ # ─────────────────────────────────────────────────────────────────────────────
39
  def fold_in_new_user(model, trainset, user_ratings, reg=5):
40
  n_factors = model.n_factors
41
  A = np.zeros((n_factors, n_factors))
 
73
  df = pd.DataFrame(movie_predictions).sort_values("rating", ascending=False).head(top_n)
74
  return df
75
 
76
+ # ─────────────────────────────────────────────────────────────────────────────
77
+ # Neural Network Recommendation
78
+ # ─────────────────────────────────────────────────────────────────────────────
79
  def recommend_with_nn(user_ratings, model, available_movies, top_n=10):
80
+ """
81
+ Args:
82
+ user_ratings: dict of movieId β†’ rating
83
+ model: compiled Keras model
84
+ available_movies: list of movieIds
85
+ top_n: number of recommendations
86
+ Returns:
87
+ DataFrame with movieId and predicted rating
88
+ """
89
+ if not available_movies:
90
+ return pd.DataFrame(columns=["movieId", "rating"])
91
+
92
+ user_id = max(user_ratings.keys(), default=0) + 100000 # Dummy new user
93
  user_vector = np.array([user_id] * len(available_movies))
94
  movie_vector = np.array(available_movies)
95
+
96
  predictions = model.predict([user_vector, movie_vector], verbose=0)
97
  df = pd.DataFrame({
98
  'movieId': available_movies,