Neighborhood-based Collaborative Filtering: Suggestion method based on nearest neighbor (P2)

Tram Ho

In part 2, I will write a demo for Neighborhood-based Collaborative Filtering (NBCF).

The theory I presented in part 1, if you want you can review it here ?

1. About distance function

In this article, I will demo with two basic distance functions, cosine and pearson

For cosine , I use cosine_similarity ‘s sklearn .

As for pearson , I use scipy ‘s pearsonr . But since this function doesn’t accept matrices (2-dimensional arrays), I have to convert it like so:

2. Building class NBCF

Initialization function:

Input parameters:

  • Y : Utility matrix, consisting of 3 columns, each column has 3 figures: user_id, item_id, rating.
  • k : The number of neighbors selected to predict the rating.
  • uuCF : If uuCF is used then uuCF = 1 , otherwise uuCF = 0 . The parameter takes a default value of 1 .
  • dist_f : The distance function, as mentioned in section 1, here I use two functions cosine and pearson . The parameter that takes the default value is the cosine_similarity function of klearn .
  • limit : The number of suggested items per user. The default is 10 .

 

Note, the NBCF class is synchronized for both iiCF and uuCF methods, so when calculating by uuCF , the Utility matrix is Y , otherwise, columns 0 and 1 of Y are swapped. user and item)

 

Standardized function

As the theory in Part 1 , each rating of each user is normalized by subtracting the average of the ratings that user has rated:

 

The function of calculating distance is similar

 

The function predicts ratings and lists items

Note, with uuCF = 0 , I will perform the function of changing two parameters u and i when performing the pred function

 

3. Algorithm evaluation

Similar to Content-base, here I also evaluate the algorithm by RMSE and precision recall. You can refer to a little:

 

So ending the 2 parts of Neighborhood-based Collaborative Filtering. As for Matrix Factorization, I’m going to end this topic. Hope that I will complete ?

Here is the source code link and reference. See you in the next article ?

Source code

Reference link

Share the news now

Source : Viblo