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?

Yes
Example

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 余弦公式的更多相关文章

  1. LintCode: Cosine Similarity

    C++ class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @retu ...

  2. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  3. 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)

    之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...

  4. cosine similarity

    Cosine similarity is a measure of similarity between two non zero vectors of an inner product space  ...

  5. 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)

    在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...

  6. Cosine Similarity of Two Vectors

    #include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...

  7. spark MLlib 概念 5: 余弦相似度(Cosine similarity)

    概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. 利用JAVA计算TFIDF和Cosine相似度-学习版本

    写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...

随机推荐

  1. 数据库ACID

    数据库的事务隔离级别 10.数据库的事务隔离级别一般分为4个级别,其中可能发生“不可重复读”的事物级别有()A.SERIALIZABLE B.READ COMMITTEDC.READ UNCOMMIT ...

  2. 29.调整数组顺序使奇数位于偶数前面[ReOrderArray]

    [题目] 输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分.要求时间复杂度为O(n). [分析] 如果不考虑时间复杂度,最简单的思路应该是从头扫描这个 ...

  3. Android 和iOS中 View的滚动

    在最近的程序中用到了Android中的View的滚动,记录一下,待总结.

  4. Mobile first! Wijmo 5 + Ionic Framework之:Hello World!

    本教程中,我们用Wijmo 5 和 Ionic Framework实现一个Mobile的工程:Hello World. Ionic是什么? Ionic是一个HTML5框架.免费.开源,用于帮助生成hy ...

  5. 集群ssh服务和免密码登录的配置

    安装Hadoop之前,由于集群中大量主机进行分布式计算需要相互进行数据通信,服务器之间的连接需要通过ssh来进行,所以要安装ssh服务,默认情况下通过ssh登录服务器需要输入用户名和密码进行连接,如果 ...

  6. php中重写和final关键字的使用

    为什么把重写和final放在一起,原因就是一条:final的意思是不可更改的,也就是说final定义的东西是不可改变的,下面具体来说一下. 来看一段简单的代码: class BaseClass { f ...

  7. ios 7.1.2 拍照声音

    打开进入文件系统(越狱)目录:/System/Library/Frameworks/MediaToolbox.framework , 重命名文件 RegionalSystemSoundsThatSha ...

  8. 使用jquery制作可视化的组织结构

    组织结构是做项目里面经常用到的,但是要做成可视化的效果比较少,多数使用树结构来表示,但是对于客户来说不是太直观.可以用jOrgChart来实现.如下图的效果,这样就比较直观. 首先你要去下载jOrgC ...

  9. DDL, DML不是所有SQL都是可以自动回滚的

    因为DDL没有事务性,所以DDL不能回滚. 要实现自动回滚.(begin,commit,rollback),则SQL语句中只能包括DML. 这样,自动化发布就会受限规范格式. 故而,一刀切的办法是,假 ...

  10. Redis/SSDB+Twemproxy的配置与使用(Mac/Linux平台)

    对于redis而已,相信不少的后台开发人员一直都在使用,相比memcache而已,redis不仅可以作为key-value缓存使用,而且提供了丰富的数据结构如set.list.map等,能够实现很多复 ...