How can I built a function that calculate cosine similarity in language R?

自古美人都是妖i 提交于 2019-12-13 00:52:48

问题


          ios d.0 d.1 d.2 d.3 d.4 d.5
1   190371877 HDa 2Pb   2 BxU BuQ Bve
2   190890807 HCK 2Pb   2  09   F   G
3   193999742 HDa 2Pb   2 1wL 1ye    
4   192348099 HDa 2Pb   2 2WP        
5   194907960 HDa 2Pb   2   Y   F   G
6   194306872 HDa 2Pb   2 2WP        
7   190571682 HDa 2Pb   2   i   F   C
8   195878080 HDa 2Pb   2   Y   F   G
9   195881580 HDa 2Pb   2   Y   F   G
10  193746161 HDa 2Pb   2 1wL     

Here is my codes below.ı just able to done for compare 2 vectors and now ı want to built a function

library('lsa')
td = tempfile()
dir.create(td)
write( c("HDa","2Pb","2","BxU","BuQ","Bve"), file=paste(td, "D1", sep="/"))
write( c("HCK","2Pb","2","09","F","G"), file=paste(td, "D2", sep="/"))


myMatrix = textmatrix(td, minWordLength=1)
res <- lsa::cosine(myMatrix[,1], myMatrix[,2])
res
#0.3333

I have a matrix 266 row and 7 column like above.First column is my product's id and the others are product's definitive tags. If ı want to have my own function and give a 2 input one is a definitive tag vector(like c("HDa","2Pb","2","BxU","BuQ","Bve")) and the other is a product id(ios). As a result I want to see true or false(1 or 0) among top 8 product which most similar to product id's vector.

Can anyone give me a clue about what should ı do for solve my problem?

来源:https://stackoverflow.com/questions/34062909/how-can-i-built-a-function-that-calculate-cosine-similarity-in-language-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!