bhoomika19 commited on
Commit
b7ca650
·
verified ·
1 Parent(s): 84a879c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -60,13 +60,13 @@ def predict_rating(user_id, movie_id):
60
  if user_id not in ratings_matrix.index or movie_id not in ratings_matrix.columns:
61
  return "Error: User ID or Movie ID not found in dataset"
62
 
63
- # Scale the data
64
- scaler = StandardScaler()
65
- ratings_scaled = scaler.fit_transform(ratings_matrix)
66
 
67
  # Apply NMF
68
- nmf = NMF(n_components=50, random_state=42)
69
- user_features = nmf.fit_transform(ratings_scaled)
70
  movie_features = nmf.components_
71
 
72
  # Get user and movie indices
@@ -76,8 +76,8 @@ def predict_rating(user_id, movie_id):
76
  # Predict rating
77
  prediction = np.dot(user_features[user_idx], movie_features[:, movie_idx])
78
 
79
- # Rescale prediction
80
- prediction = scaler.inverse_transform([[prediction]])[0][0]
81
  prediction = max(0.5, min(5, prediction)) # Clip between 0.5 and 5
82
 
83
  return f"🎬 Predicted Rating: {prediction:.2f} / 5.0"
 
60
  if user_id not in ratings_matrix.index or movie_id not in ratings_matrix.columns:
61
  return "Error: User ID or Movie ID not found in dataset"
62
 
63
+ # Instead of StandardScaler, we'll normalize the data to [0,1] range
64
+ ratings_array = ratings_matrix.values
65
+ ratings_normalized = (ratings_array - ratings_array.min()) / (ratings_array.max() - ratings_array.min())
66
 
67
  # Apply NMF
68
+ nmf = NMF(n_components=50, init='random', random_state=42)
69
+ user_features = nmf.fit_transform(ratings_normalized)
70
  movie_features = nmf.components_
71
 
72
  # Get user and movie indices
 
76
  # Predict rating
77
  prediction = np.dot(user_features[user_idx], movie_features[:, movie_idx])
78
 
79
+ # Scale prediction back to original range [0.5, 5]
80
+ prediction = (prediction * (5 - 0.5)) + 0.5
81
  prediction = max(0.5, min(5, prediction)) # Clip between 0.5 and 5
82
 
83
  return f"🎬 Predicted Rating: {prediction:.2f} / 5.0"