Untitled

mail@pastecode.io avatar
unknown
python
22 days ago
910 B
2
Indexable
Never
data_matrix = df.pivot_table(index='userId', columns='movieId', values='rating')
logging.info("Data Matrix created")

predicted_ratings = []

for i, user in enumerate(set(users)):
    for movie in set(movies):
        if np.isnan(data_matrix.at[user, movie]):
            predicted_rating = algo.predict(user, movie)[3]
            if not np.isnan(predicted_rating):
                predicted_rating = round(min(max(predicted_rating, 1.0), 5.0) / 0.5) * 0.5
                predicted_ratings.append((user, movie, predicted_rating))
            else:
              logging.info(f"For user {user} and movie {movie} have empty rating {predicted_rating}")
        else:
            predicted_ratings.append((user, movie, data_matrix.at[user, movie]))

    logging.info(f"Worked {i+1} users of {len(set(users))}")

return pd.DataFrame(predicted_ratings, columns=['userId', 'movieId', 'rating'])
Leave a Comment