#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. js 性能调试

    今天有幸偶遇我早就神往已久的性能调试问题. 原来js调试工具里面有可以记录每个方法的执行时间的功能,站在此功能的肩膀上就可以对自己的程序性能.瓶颈了如指掌,就可以针对性的,瞄准目标,斩草除根,以绝后患 ...

  2. ZipMarket数字内容/素材交易网站源码项目

    ZipMarket程序仿自Envato旗下网站,对于想创建数字内容/素材交易平台的站长来说,ZipMarket是一个十分独特和极具创新的解决方案,用户在你的网站注册并购买或出售数字内容/素材作品时,你 ...

  3. SQL Server建库-建表-建约束

    ----------------------------------------SQL Server建库-建表-建约束创建School数据库------------------------------ ...

  4. Android Service初解

    Service是什么呢? 他同Activity相比,最大的不同就是他没有专门的Layout展示界面,他默默的工作在App的后台. 虽然除了少数几种情况,我们不需要使用Service,但我们也有必要了解 ...

  5. session_start(): open(/var/lib/php/session/sess_tlrp3cpro7gun9uhno8n6futr3, O_RDWR)

    在安装一个网站,结果连接上数据库了但是一直报错.下面贴报错内容和图 错误提示 系统:PHP (Linux) 错误类型:WARNING 内容:session_start(): open(/) 错误位置: ...

  6. Python之进程 基础知识 上

    阅读目录 理论知识 操作系统背景知识 什么是进程 进程调度 进程的并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 在python程序中的进程操作 multiprocess模块 进程的创建和mu ...

  7. BZOJ 2626: JZPFAR KDtree + 堆

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000000000 #define mid ((l ...

  8. [C++] muParser 的简单使用方法

    关于 muParser 库 许多应用程序需要解析数学表达式.该库的主要目的是提供一种快速简便的方法. muParser是一个用C ++编写的可扩展的高性能数学表达式解析器库. 它的工作原理是将数学表达 ...

  9. APK动态加载框架 https://github.com/singwhatiwanna/dynamic-load-apk

    https://github.com/singwhatiwanna/dynamic-load-apk

  10. Restful传递数组参数的两种方式

    第一种,直接传递数组 js直接传递数组 var data = ["123","456"];that.loadDictionarys(data).subscrib ...