Building Recommendation Engines in Python
Rob O'Callaghan
Director of Data
print(user_ratings_pivot):
The Great Gatsby The Catcher in the Rye Fifty Shades of Grey
User_233 0.0 0.0 0.0
User_651 0.0 0.5 -0.5
User_965 0.5 -0.5 0.0
... ... ... ...
book_ratings_pivot = user_ratings_pivot.T
print(book_ratings_pivot)
User_233 User_651 User_965
The Great Gatsby 0.0 0.0 0.5
The Catcher in the Rye 0.0 0.5 -0.5
Fifty Shades of Grey 0.0 -0.5 0.0
... ... ... ...
book_ratings_pivot
:
User_233 User_651 User_965
The Great Gatsby 0.0 0.0 0.5
The Catcher in the Rye 0.0 0.5 -0.5
Fifty Shades of Grey 0.0 -0.5 0.0
... ... ... ...
cosine_similarity( ,
)
cosine_similarity(book_ratings_pivot.loc['Lord of the Rings', :] ,
book_ratings_pivot.loc['The Hobbit', :] )
cosine_similarity(book_ratings_pivot.loc['Lord of the Rings', :].values ,
book_ratings_pivot.loc['The Hobbit', :].values )
cosine_similarity(book_ratings_pivot.loc['Lord of the Rings', :].values.reshape(1, -1),
book_ratings_pivot.loc['The Hobbit', :].values.reshape(1, -1))
0.43
cosine_similarity(book_ratngs.loc['Lord of the Rings', :].values.reshape(1, -1),
book_ratngs.loc['Twilight', :].values.reshape(1, -1))
-0.64
similarities = cosine_similarity(book_ratings_pivot)
cosine_similarity_df = pd.DataFrame(book_ratings_pivot, index=book_ratings_pivot.index, columns=book_ratings_pivot.index)
cosine_similarity_df.head()
The Great Gatsby The Catcher in the Rye Fifty Shades of Grey
The Great Gatsby 1.0 0.0 -0.3
The Catcher in the Rye 0.0 1.0 -0.5
Fifty Shades of Grey -0.3 -0.5 1.0
... ... ... ...
cosine_similarity_series = cosine_similarity_df.loc['The Hobbit']
ordered_similarities = cosine_similarity_series.sort_values(ascending=False)
print(ordered_similarities)
The Hobbit 1.00
Lord of the Rings 0.43
The Silmarillion 0.37
...
Building Recommendation Engines in Python