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);
    }
}

你可能感兴趣的:(Cosine Similarity)