#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>

template <class T>
double VectorCosine(const std::vector<T> &In1, const std::vector<T> &In2) {
    if(In1.size() != In2.size()) {
        return -2;
    }
    double la=sqrt(inner_product(In1.begin(), In1.end(), In1.begin(), 0));
    double lb=sqrt(inner_product(In2.begin(), In2.end(), In2.begin(), 0));
    double val=inner_product(In1.begin(), In1.end(), In2.begin(), 0);

if((std::abs(la-0.0)<0.0001) || std::abs(lb-0.0)<0.0001)
        return -3;

return val/(la*lb);
}

int main() {
    int a[]={3, 2, 0, 5, 0, 0, 0, 2, 0, 0}, b[]={1, 0, 0, 0, 0, 0, 0, 1, 0, 2};
    std::vector<int> v_a(a, a+sizeof(a)/sizeof(a[0])), v_b(b, b+sizeof(b)/sizeof(b[0]));

double s=VectorCosine(v_a, v_b);

std::cout<<s<<std::endl;

return 0;
}

Cosine similarity really is a measure of the(cosine of the) angle between x and y. Thus, if the cosine similarity is 1,

the angle between x and y is 0, and x and y are the same except for magnitude(length). If the cosine similarity is 0,

then the angle between x and y is 90, and they do not share any terms.

Cosine Similarity of Two Vectors的更多相关文章

  1. cosine similarity

    Cosine similarity is a measure of similarity between two non zero vectors of an inner product space  ...

  2. [LintCode] Cosine Similarity 余弦公式

    Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...

  3. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  4. spark MLlib 概念 5: 余弦相似度(Cosine similarity)

    概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...

  5. 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)

    在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...

  6. 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)

    之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...

  7. LintCode: Cosine Similarity

    C++ class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @retu ...

  8. 利用JAVA计算TFIDF和Cosine相似度-学习版本

    写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...

  9. python之验证码识别 特征向量提取和余弦相似性比较

    0.目录 1.参考2.没事画个流程图3.完整代码4.改进方向 1.参考 https://en.wikipedia.org/wiki/Cosine_similarity https://zh.wikip ...

随机推荐

  1. CSS固定布局:960GS

    960栅格系统 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. yum进程被占用

    使用yum安装软件的时候出现,/var/run/yum.pid 已被锁定,PID 为 6503 的另一个程序正在运行的问题 [root@localhost mysql]# yum install gc ...

  3. DECLARE_DYNAMIC

    DECLARE_DYNAMIC(class_name) DECLARE_DYNCREATE 包含了DECLARE_DYNAMIC的功能,并且可以在运行过程中动态创建对象.如果需要动态创建类对象,需要使 ...

  4. php第十四节课

    投票 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  5. Django的基础教程

    学Django需要什么基础? 1. Django是 python 语言写的一个Web框架包,所以你得知道一些 Python 基础知识. 2. 其次你最好有一些做网站的经验,懂一些网页 HTML, CS ...

  6. BZOJ 1051 HAOI 2006 受欢迎的牛

    [题解] 先用tarjan缩点,然后如果某个强联通分量的出度为0,则该强联通分量内的点数为答案,否则无解.因为若其他所有的强联通分量都有边连向Ai,则Ai必定没有出边,否则Ai连向的点所属的强联通分量 ...

  7. hdu 5175 Misaki's Kiss again

    Misaki's Kiss again  Accepts: 75  Submissions: 593  Time Limit: 2000/1000 MS (Java/Others)  Memory L ...

  8. SqlServer转换为Mysql(mss2sql)

    SqlServer转换为Mysql(mss2sql)工具 http://pan.baidu.com/s/1c2d8R8O 参考链接: http://www.cnblogs.com/angestudy/ ...

  9. hdu2004 成绩转换【C++】

    成绩转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. Pyspider爬虫简单框架——链家网

    pyspider 目录 pyspider简单介绍 pyspider的使用 实战 pyspider简单介绍 一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构, ...