User-based collaborative filtering and recommendations

In order to build a user-based collaborative filtering algorithm, we need to compute cosine similarities between users. Let's take a look at the following code:

user_user_sim_matrix = pd.DataFrame(    cosine_similarity(customer_item_matrix))

As is noticeable from this code, we are using the cosine_similarity function from the sklearn package's metrics.pairwise module. This function computes pairwise cosine similarities between the samples and outputs the results as an array type. Then, we create a pandas DataFrame with this output array and store it into a variable named user_user_sim_matrix, which stands for user-to-user similarity matrix. The result looks as follows:

As you can see from ...

Get Hands-On Data Science for Marketing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.