Find cosine similarity between two arrays

后端 未结 6 1483
忘掉有多难
忘掉有多难 2020-11-27 13:28

I\'m wondering if there is a built in function in R that can find the cosine similarity (or cosine distance) between two arrays?

Currently, I implemented my own func

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 13:44

    Taking the comment from Jonathan Chang I wrote this function to mimic dist. No extra packages to load.

    cosineDist <- function(x){
      as.dist(1 - x%*%t(x)/(sqrt(rowSums(x^2) %*% t(rowSums(x^2))))) 
    }
    

提交回复
热议问题