You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# model = movie similarities matrix
# movie_id = target movie id
# ratings = dict of movie_id: rating
rated_movies = ratings.keys()
similar_movies = model[movie_id, rated_movies].argsort()[:-1]
top_n = [ratings.keys()[i] for i in similar_movies[:n]]
# Average rating weighted by similarity
scores = sum(model[movie_id, m] * ratings[m] for m in top_n)
prediction = float(scores) / sum(model[movie_id, m] for m in top_n)
return prediction
It seems you are having trouble making the Cosine Similarity notebook work.
The code in this repository is at least two years old and written in python 2.7. Since some Python/SciPy/NumPy interfaces and defaults have changed, parts of the code will break if you run it with the latest Python/SciPy/NumPy versions.
In this particular case, the keys method of a dict returns a list in Python 2 and an object of dict_keys in Python 3. NumPy Arrays can be indexed by lists but not by dict_keys objects. Hence, you get an error. Replacing rated_movies = ratings.keys() with rated_movies = list(ratings.keys()) and top_n = [ratings.keys()[i] for i in similar_movies[:n]]with top_n = [rated_movies[i] for i in similar_movies[:n]] should make the notebook work with Python 3.
Please see the error:
IndexError Traceback (most recent call last)
in ()
10
11 for movie_id in (10,100,1000):
---> 12 print(movie_id,predict_rating(similarities, user_ratings, movie_id))
in predict_rating(model, ratings, movie_id, n)
9
10 rated_movies = ratings.keys()
---> 11 similar_movies = model[movie_id, rated_movies].argsort()[:-1]
12 top_n = [ratings.keys()[i] for i in similar_movies[:n]]
13
IndexError: only integers, slices (
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices------------------------------------------------------Cosine Similarity
import numpy as np
def predict_rating(model, ratings, movie_id, n=5):
user_id = 10
movies_rated = np.where(R[user_id].todense() > 0)[1].tolist()
movie_ratings = R[user_id, movies_rated].todense().tolist()[0]
user_ratings = dict(zip(movies_rated, movie_ratings))
print('10 2.77208508312')
print('100 1.77135318293')
print('1000 2.35262213947')
for movie_id in (10,100,1000):
print(movie_id,predict_rating(similarities, user_ratings, movie_id))
The text was updated successfully, but these errors were encountered: