[LintCode] Cosine Similarity 余弦公式
Cosine similarity is a measure of similarity between two vectors of an inner product space that measures the cosine of the angle between them. The cosine of 0° is 1, and it is less than 1 for any other angle.
See wiki: Cosine Similarity
Here is the formula:

Given two vectors A and B with the same size, calculate the cosine similarity.
Return 2.0000 if cosine similarity is invalid (for example A = [0] and B = [0]).
Have you met this question in a real interview?
Given A = [1, 2, 3], B = [2, 3 ,4].
Return 0.9926.
Given A = [0], B = [0].
Return 2.0000
这道题让我们求两个向量之间的余弦值,而且给了我们余弦公式,唯一要注意的就是当余弦值不存在时,返回2.0,其余的照公式写即可,参见代码如下:
class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: Cosine similarity.
*/
double cosineSimilarity(vector<int> A, vector<int> B) {
// write your code here
double nA = norm(A), nB = norm(B), m = ;
if (nA == || nB == ) return 2.0;
for (int i = ; i < A.size(); ++i) {
m += A[i] * B[i];
}
return m / (nA * nB);
}
double norm(vector<int> V) {
int res = ;
for (int i = ; i < V.size(); ++i) {
res += V[i] * V[i];
}
return sqrt(res);
}
};
[LintCode] Cosine Similarity 余弦公式的更多相关文章
- LintCode: Cosine Similarity
C++ class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @retu ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)
之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...
- cosine similarity
Cosine similarity is a measure of similarity between two non zero vectors of an inner product space ...
- 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)
在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...
- Cosine Similarity of Two Vectors
#include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...
- spark MLlib 概念 5: 余弦相似度(Cosine similarity)
概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 利用JAVA计算TFIDF和Cosine相似度-学习版本
写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...
随机推荐
- CheckBoxList1复选框
循环绑定数据的两个方法: List<string> LIColl = new List<string>(); protected void Page_Load(object s ...
- python 将pdf分页后插入至word中
所用技术 1. python编程基础 2. 使用pyPdf 3. 使用python操作word 4. 正则表达式的使用 5. windows的bat编程 下面是一个pyPdf库使用的示例: from ...
- 【python】lamda表达式,map
一个很好的博客:http://blog.csdn.net/mathboylinlin/article/details/9413551 博客不让转载,我只摘抄了里面几个例子,更多内容到博客里去看 lam ...
- Servlet过滤器和监听器
1,Servlet过滤器 <filter> <filter-name>charset</filter-name> <filter-class>org.g ...
- Linux设置IP
进入 vi /etc/sysconfig/network-scripts/ifcfg-eth0 root # ifconfig eth0 192.168.22.232 root # route ad ...
- LINUX下安装TeamViewer
https://www.teamviewer.com/zhcn/download/linux/ [root@ok teamviewer]# yum install -y teamviewer_11.0 ...
- iptables 无法连外网
[root@v01-svn-test-server ~]# service iptables status Table: filter Chain INPUT (policy DROP) num ta ...
- 用sqlplus登陆数据库时,oracle 11g出现ORA-12514问题
转自:http://zhidao.baidu.com/question/144648216.html 启动服务 然后在sqlplus / as sysdba;执行启动startup nomount;a ...
- SQL Server连接数据库失败,可能的问题!
SQL Server Configuration Manager中启动服务 SQL Server外围应用配置器中,打开远程IP连接属性 别的应该没什么问题了!
- hive复杂类型与java类型的对应
因为要往自定义的UDF传入复杂类型,所以需要对于这块的对应简单做一下总结 string java.lang.String, org.apache.hadoop.io.Text int int, jav ...