#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. SQL基本操作——UNION

    UNION 操作符:用于合并两个或多个 SELECT 语句的结果集.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...

  2. 我的liunx开发环境的配置之路

    相信有不少人和我一样,虽然是做纯linux开发,但并不排斥windows,并且喜欢在windows下面的使用各种好用的工具来让linux的编程工作变得更加方便.实际上每一个系统都有他的过人支持,win ...

  3. MongoDB主库和从库的数据大小不一致原因判断

    1. 环境(MongoDB的版本是3.2.16) [root@xxx-mongodb-primary ~]# cat /etc/redhat-release CentOS Linux release ...

  4. mac上常用的命令

    平时会经常遇到的问题做一个总结

  5. JavaScript初步学习----基本使用,简单事件,修改样式,数据类型

    JavaScript基本使用 JavaScript原名叫livescript,是一门动态类型,弱类型基于原型的脚本语言   用于页面特效,前后交替,后台开发(node)   JavaScript写在s ...

  6. 【郑轻邀请赛 F】 Tmk吃汤饭

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2132 [题意] [题解] 很容易想到用队列来模拟; 这个队列维护的是正在煮的4个人煮 ...

  7. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  8. 玲珑杯1147 - 最后你还是AK了

    1147 - 最后你还是AK了 Time Limit:5s Memory Limit:256MByte DESCRIPTION 今天HHHH遇到了一颗树,这个树有nn个点(nn为偶数),每条边都有一个 ...

  9. MINSUB - Largest Submatrix

    MINSUB - Largest Submatrix no tags  You are given an matrix M (consisting of nonnegative integers) a ...

  10. Python 3 条件语句

    条件语句:  用于判定,判定是否符合某条件,符合则执行,不符合则不执行该条件所定义的操作. 一步判定:  用于理解不会这样使用. if  1==1:    if条件判定只能出现一次. print(&q ...