Update app.py
Browse files
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 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
# Apply NMF
|
| 68 |
-
nmf = NMF(n_components=50, random_state=42)
|
| 69 |
-
user_features = nmf.fit_transform(
|
| 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 |
-
#
|
| 80 |
-
prediction =
|
| 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"
|