Tuesday, November 1, 2022

[FIXED] How to compute cosine for loop with function from *apply family?

Issue

I need convert this for loop into small because it's taking more time for 50k accounts and I need to improve performance by using a function from the *apply family.

a <- data.frame(a=c(1, 2, 2, 3), b=c(2, 3, 4, 5))
b <- data.frame(a=c(1), b=c(2))

##### **by using apply function I need convert this code**
library('lsa')
res <- c()
for (i in 1:nrow(a)) {
  cosinevalue <- cosine(as.numeric(a[i, ]), as.numeric(b))[1]
  res <- rbind(res, as.numeric(cosinevalue))
}

a$cosinevalue <- res

Solution

Using vapply.

vapply(seq_len(nrow(a)), \(i) lsa::cosine(unlist(a[i, ]), unlist(b)), numeric(1))
# [1] 1.0000000 0.9922779 1.0000000 0.9970545

Or Vectorizeing it, which is three times faster.

Vectorize(lsa::cosine, vectorize.args='x')(as.data.frame(t(a)), unlist(b)) |> unname()
# [1] 1.0000000 0.9922779 1.0000000 0.9970545


Answered By - jay.sf
Answer Checked By - Marie Seifert (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.