Cosine Similarity of Two Vectors
#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的更多相关文章
- cosine similarity
Cosine similarity is a measure of similarity between two non zero vectors of an inner product space ...
- [LintCode] Cosine Similarity 余弦公式
Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- spark MLlib 概念 5: 余弦相似度(Cosine similarity)
概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...
- 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)
在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...
- 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)
之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...
- LintCode: Cosine Similarity
C++ class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @retu ...
- 利用JAVA计算TFIDF和Cosine相似度-学习版本
写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...
- python之验证码识别 特征向量提取和余弦相似性比较
0.目录 1.参考2.没事画个流程图3.完整代码4.改进方向 1.参考 https://en.wikipedia.org/wiki/Cosine_similarity https://zh.wikip ...
随机推荐
- JS——大小写转化
<script> var str = 'JavaScript'; console.log(str.toUpperCase());//小写转大写 console.log(str.toLowe ...
- Linux监控实时log
https://jingyan.baidu.com/article/93f9803f5545a3e0e46f5596.html
- iOS 9 中可用的受信任根证书列表
iOS 受信任证书存储区包含随 iOS 预安装的可信根证书. https://support.apple.com/zh-cn/HT205205 关于信任和证书 iOS 9 受信任证书存储区包含三类证 ...
- 取textaera里的值
jQuery的.val()方法是专门用来获取表单元素值的,而textarea也属于表单元素所以可以直接用.val()方法获取. 不过要注意,由于textarea是个双标记因此.text()或.html ...
- 文章或者观点说说等点赞功能实现(thinkphp)
前端的代码: <!-- 点赞 --> <div class='btm'><a class='zan' id="{$article.id}" href= ...
- cf 337 div2 c
C. Harmony Analysis time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 【JavaScript进阶】深入理解JavaScript中ES6的Promise的作用并实现一个自己的Promise
1.Promise的基本使用 // 需求分析: 封装一个方法用于读取文件路径,返回文件内容 const fs = require('fs'); const path = require('path') ...
- uva-156(Ananagrams UVA - 156)
map容器的模板题,判断是否能交换字母顺序变成另外一个单词,只需要先把单词都变成小写字母.然后再按字母字典序排序,放入map中进行计数,然后把计数为一的再放入另一个容器,再排序输出即可 我的代码(刘汝 ...
- 使用redis和简单token机制校验身份的思路
1. 登录时生成token, 以token为键,以用户信息为值,存储在redis中,设置key过期时间 2. 需要身份验证的接口,带上token 3. 接口校验redis中token是否存在 4. 存 ...
- 36.分组聚合操作—bucket进行多层嵌套
主要知识点: 分组聚合操作-嵌套bucket. 本讲以前面电商实例,从颜色到品牌进行下钻分析,每种颜色的平均价格,以及找到每种颜色每个品牌的平均价格. 比如说,现在红色的电视有4台,同 ...