In the user-based CF method, we get the recommendations based on the computation of the similarity between the users. There are different methods to compute the similarity between the users. We will see the implementation of similarity computation using the cosine
method.
The following function computes the cosine similarity between the users. The logic to compute the cosine similarity is mentioned here, but in order to know more about the cosine similarity methodology, refer to the online articles pertaining to cosine similarity. We need to pass the user IDs to be compared to the function. First, we convert the data into a data frame format and omit the products that were not purchased by both the users as including them would bias the similarity score. Then, we compute the cosine similarity for the users. If the pair of users has not bought at least three common products, then we will not be computing the similarity but instead, will make it as zero:
...