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 ...
随机推荐
- Java 基础入门随笔(2) JavaSE版——关键字、进制转换、类型转换
1.Java语言-关键字 关键字:被java语言赋予了特殊含义的词,特点是所有的字母都为小写. java涉及到的关键字整理: 用于定义数据类型的关键字 class interface byte sho ...
- 2016.01.22 前端学习 HTML/CSS
学习HTML/CSS http://edu.51cto.com/course/course_id-3116.html 明日实践
- js中关于new Object时传参的一些细节分析
1, 参数是一个对象,核心js对象(native ECMAScript object)或宿主对象(host object),那么将直接返回该对象. 其生成的对象构造器仍然是所传参数对象的构造器.这样造 ...
- python开发 面试题
一.简述列表与元组的区别 答: 元组tuple与列表List相同点 元组tuple与列表List都是序列类型的容器对象,可以存放任何类型的数据.支持切片.迭代等操作. 元组tuple与列表List区别 ...
- vue 打印 页面特定部分转pdf
https://www.jb51.net/article/147040.htm https://www.jianshu.com/p/dd120b65446a //转pdf
- 点击 table 单元格 取值
function Test() { var rows = document.getElementById("tbDetail").rows; if (rows.length > ...
- node版本管理工具nvm安装使用教程
一些安装包依赖一定的node版本,可以采用nvm管理node, 可以快速的进行版本切换. 操作系统: windows10, x64 常见版本工具: 1. nvmw, nvmm install node ...
- ES6学习历程(字符串的扩展)
字符串的扩展 在看这一节的时候前半部分写的都是关于unicode的内容,我个人感觉这部分在实际的开发中用的很少,所以不打算在做记录,等届时用到再有针对性的看,所以就将在ES6里面关于字符串操作的一些新 ...
- [luogu1485 HNOI2009] 有趣的数列 (组合数学 卡特兰数)
传送门 Solution 卡特兰数 排队问题的简单变化 答案为\(C_{2n}^n \pmod p\) 由于没有逆元,只好用分解质因数,易证可以整除 Code //By Menteur_Hxy #in ...
- 第三节:Web爬虫之BeautifulSoup解析库
Beautiful Soup官方说明: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为 ...