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. wsp反编译

    最后出于好奇,我把wsp文件解压缩,看看里面是什么(如果您的机器上的压缩软件不能直接解压,可尝试修改后缀名为cab.).我看到的首先是一个清单文件(manifest.xml),一个DLL文件(Shar ...

  2. iOS 图片拉伸的解释

    以前对于ios的图片拉伸参数一直不太理解,终于看到一篇好文章,转载一下,原文地址:http://blog.csdn.net/q199109106q/article/details/8615661 主要 ...

  3. iOS7 status bar 样式问题

    在ios7中,有如下status bar 样式 typedef NS_ENUM(NSInteger, UIStatusBarStyle) { UIStatusBarStyleDefault = , / ...

  4. Android 和iOS 创建本地通知

    1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...

  5. iOS 定制controller过渡动画 ViewController Custom Transition使用体会

    最近学习了一下ios7比较重要的一项功能,就是 controller 的 custom transition. 在ios7中,navigation controller 中就使用了交互式过渡来返回上级 ...

  6. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  7. fcitx-sogoupinyin下载地址和安装

    伴随着Deepin 12.12 beta的发布,搜狗输入法也与我们见面了.在发布前几日Deepiner也通过各种途径向我们展示了搜狗Linux输入法,当然也掉足了胃口. 来自官方的截图: 当然令很多U ...

  8. JDK1.7 ConcurrentHashMap 源码浅析

    概述 ConcurrentHashMap是HashMap的线程安全版本,使用了分段加锁的方案,在高并发时有比较好的性能. 本文分析JDK1.7中ConcurrentHashMap的实现. 正文 Con ...

  9. 一台机器开2个Tomcat修改端口号

    修改一个Tomcat端口号步骤:1.找到Tomcat目录下的conf文件夹2.进入conf文件夹里面找到server.xml文件3.打开server.xml文件4.在server.xml文件里面找到下 ...

  10. Java虚拟机支持的最大内存限制

    最近在开发Java的程序.本来我是一直很喜欢Java的内存管理的,不需要担心分配内存,只管分配,垃圾收集器自己会给你回收内存的.现在开发的程序数据量很大,为了速度快,我准备把所有的信息加载进内存,这样 ...