Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices #4

Closed
shubhamtunwal97 opened this issue Oct 31, 2018 · 2 comments

Comments

@shubhamtunwal97
Copy link

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):

# 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

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))

@saurabhmathur96
Copy link
Owner

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.

@shubhamtunwal97
Copy link
Author

Thanks it worked.!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants