Zlorpo123
Zlorpo123

Reputation: 47

Writing an R function to find cosine similarity

I'm aware of the cosine function that can be used to find cosine similarity that exists in one of the R Studio libraries. I'm attempting to make my own using vectorized operators but I'm stumped. My function seemingly adds all the values together then does the operations on them in one time instead of doing each cosine similarity on each point set and adding them together like it should.

cossim <- function(A,B) { (sum(A,B))/sqrt((sum(A^2))*(sum(B^2))) }

I pass in two vectors(for testing purposes I just tried passing in the same vector to see if it would return 1 which is what happens when you do cosine on 2 of the same vector) and it would return 0.8571429 instead of 1.

vec1 <- c(1,2,3)

When I did the math, the function ended up with 12/14 instead of 14/14 like how it should at the end to get 1.

Upvotes: 0

Views: 910

Answers (1)

Leonardo Viotti
Leonardo Viotti

Reputation: 506

I think the issue is just that you should be multiplying the elements of A and B before summing in the numerator.

cossim <- function(A,B) { (sum(A*B))/sqrt((sum(A^2))*(sum(B^2))) }

Upvotes: 2

Related Questions