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 ...
随机推荐
- 三角形状的点阵模糊效果iOS源码
源码FFAngularPointilism,FFAngularPointilism能够将UIImageView像添加滤波器一样生成三角形状的点阵模糊效果.可以通过动画方式来模糊,也可以立刻模糊.另外并 ...
- mha0.56版本安装使用排错
1.master_check_ssh --conf=/etc/app1.conf 这个检查就报错的我觉得百分之九十都是ssh之间连接问题.务必要保证各节点之间都可以免秘钥访问! 2.mas ...
- php省市区三级联动
效果 步骤 前端:通过ajax请求获取数据,使用了jquery 页面一开始加载所有省份信息 ->当选择省下拉框后触发改变监听时间-change ->当选择市下拉框后触发改变监听时间-cha ...
- swift 再识枚举变量
// Use enum to create an enumeration. Like classes and all other named types, enumerations can have ...
- [数据结构】【c语言】链表的创建和遍历
第一次写代码的博客,一个刚刚接触的新手,来这里主要是为了记录自己,方便自己以后浏览,也欢迎大家指正.先来个简单的,动态链表的创建和遍历. #include<stdio.h> #includ ...
- 匈牙利算法求最大匹配(HDU-4185 Oil Skimming)
如下图:要求最多可以凑成多少对对象 大佬博客: https://blog.csdn.net/cillyb/article/details/55511666 https://blog.csdn.net/ ...
- NOIP2018 滚粗记
Day -2 上午,大家都在复习各种模板,zhx总结了足足67个模板(杨辉三角也算模板???),lgl死磕FFT发现cos和sin打反了,我也是复习板子和以前做过的题,几乎没有人颓. 接着jdr,l ...
- 模拟Spring容器的getBean方法(Maven工程)
Spring容器的getBean方法是通过反射机制实现的,下面的测试程序模拟getBean的实现原理. 步骤一:pom.xml文件配置解析XML文件的dom4j.jar 步骤二:XML文件中配置bea ...
- 18.match_phrase的用法
主要知识点: match_phrase的使用场景 match_phrase的用法 match_phrase的原理 一.什么是近似匹配 match_phrase的使用场景 现假设有两个句子 ...
- ZOJ 5579 Stean
Stean Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Tom is good at making stea ...