Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

r - Find cosine similarity between two arrays

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 function, but I can't help but think that R should already come with one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

These sort of questions come up all the time (for me--and as evidenced by the r-tagged SO question list--others as well):

is there a function, either in R core or in any R Package, that does x? and if so,

where can i find it among the +2000 R Packages in CRAN?

short answer: give the sos package a try when these sort of questions come up

One of the earlier answers gave cosine along with a link to its help page. This is probably exactly what the OP wants. When you look at the linked-to page you see that this function is in the lsa package.

But how would you find this function if you didn't already know which Package to look for it in?

you can always try the standard R help functions (">" below just means the R command line):

> ?<some_name>

> ??<some_name>

> *apropos*<some_name>

if these fail, then install & load the sos package, then

***findFn***

findFn is also aliased to "???", though i don't often use that because i don't think you can pass in arguments other than the function name

for the question here, try this:

> library(sos)

> findFn("cosine", maxPages=2, sortby="MaxScore")

The additional arguments passed in ("maxPages=2" and "sortby="MaxScore") just limits the number of results returned, and specifies how the results are ranked, respectively--ie, "find a function named 'cosine' or that has the term 'cosine' in the function description, only return two pages of results, and order them by descending relevance score"

The findFn call above returns a data frame with nine columns and the results as rows--rendered as HTML.

Scanning the last column, Description and Link, item (row) 21 you find:

Cosine Measures (Matrices)

this text is also a link; clicking on it takes you to the help page for that function in the Package which contains that function--in other words

using findFn, you can pretty quickly find the function you want even though you have no idea which Package it's in


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...