Search code examples
recommendation-enginecosine-similarity

In CF item-item recommenders, how can I calculate item similarity when the matrix is sparse?


On the way to find item neighbors, I need first to calculate similarity. How can I calculate it in sparse matrix? Is it correct?


Solution

  • In item-based collaborative filtering we calculate the similarity between items.

    Here we can use cosine similarity because no matter how much sparse vectors are, Cosine similarity will calculate neighbors based on cosine angle between the vectors or the closeness of two vectors in the vector space. Not on the basis of the values of thè vectors. For example:-

            Per1    Per2    Per3
    Item1    5        3        1
    Ttem2    2        3        3
    

    If we calculate the cosine similarity of two vectors:

    Cos_sim_1 = (5*2 + 3*3 + 1*3) / sqrt((25+9+1)*(4+9+9)
    Cos_sim_1 = 0.792
    

    And if the matrix is sparse:

            Per1    Per2    Per3    Per4    Per5    Per6    Per7    per8
    Item1    5        3        1     0       0        0       0       0    
    Ttem2    2        3        3     0       0        0       0       0
    

    And the cosine similarity of sparse vectors:

    Cos_sim_2 = (5*2 + 3*3 + 1*3 + 0*0 + 0*0 +0*0 +0*0 +0*0) / sqrt((25+9+1+0+0+0+0+0)*(4+9+9+0+0+0+0+0))
    Cos_sim_2 = 0.792
    

    I hope it helps!!!!! enter image description here