Search code examples
rmatrixsimilarityrecommendation-enginecosine-similarity

Calculate similarity matrix for the 1st column


I have started working on a few ML projects and use R as the preferred language. I am trying to build a basic recommendation system

http://www.dataperspective.info/2014/05/basic-recommendation-engine-using-r.html

I need to find the similarity matrix (according to the website) and using cosine function (in 'lsa' package) to find user_similarity.

library(lsa)
data_rating <- read.csv("recommendation_basic1.csv", header = TRUE)

x = data_rating[,2:7]
x[is.na(x)] = 0
print(x)

similarity_users <- cosine(as.matrix(x))
similarity_users

But I need to find the similarity matrix among users and this code is giving me an output similarity matrix among the movies. Do I need to modify the below line?

x = data_rating[,2:7]

PS. The recommendation_basic1.csv is the same as in the link.


Solution

  • Putting this in so the question is not unanswered.

    You can just use similarity_users <- cosine(as.matrix(t(x)))
    Here, the t is matrix transpose, so it just switches the rows and columns which is equivalent to switching the users and the movies.