Cosine Similarity

Cosine Similarity

这是该公众号第一次推送算法题

选择了一个完全没有难度的题目,该题目也是lintcode的第一题,示例题目。相信选取这个题目会是一个良好的开端。

题目如下:


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:

cosine-similarity.png

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]).
Example
Given A = [1, 2, 3], B = [2, 3 ,4].
Return 0.9926.
Given A = [0], B = [0].
Return 2.0000

余弦相似度是机器学习中的一个重要概念,在Mahout等MLlib中有几种常用的相似度计算方法,如欧氏相似度,皮尔逊相似度,余弦相似度,Tanimoto相似度等。其中,余弦相似度是其中重要的一种。

代码如下:

java版


class Solution {
    /**
     * @param A: An integer array.
     * @param B: An integer array.
     * @return: Cosine similarity.
     */
    public double cosineSimilarity(int[] A, int[] B) {
        // write your code here
        if(A.length != B.length)
            return 2.0;
        double ab = 0, a = 0, b = 0;
        for(int i = 0; i < A.length; i++){
            a += A[i] * A[i];
            b += B[i] * B[i];
            ab += A[i] * B[i];
        }
        if(a == 0 || b == 0)
            return 2.0;
        return ab / Math.sqrt(a) / Math.sqrt(b);
    }
}

如果觉得文章有帮助的话,不妨关注一下本公众号,当然也希望能帮作者推广一下,更多人关注我也会更有动力,在此先谢过了。

关注我

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 不知道为何,每每遇到心烦气躁的事情,你就会来到我的梦里,一起温习着旧时光。 那时的你笑容满面,低头计算着手...
    小团长阅读 217评论 0 0