Cosine Similarity

Cosine Similarity

這是該公眾號(hào)第一次推送算法題

選擇了一個(gè)完全沒(méi)有難度的題目,該題目也是lintcode的第一題,示例題目。相信選取這個(gè)題目會(huì)是一個(gè)良好的開(kāi)端。

題目如下:


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

余弦相似度是機(jī)器學(xué)習(xí)中的一個(gè)重要概念,在Mahout等MLlib中有幾種常用的相似度計(jì)算方法,如歐氏相似度,皮爾遜相似度,余弦相似度,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);
    }
}

如果覺(jué)得文章有幫助的話,不妨關(guān)注一下本公眾號(hào),當(dāng)然也希望能幫作者推廣一下,更多人關(guān)注我也會(huì)更有動(dòng)力,在此先謝過(guò)了。

關(guān)注我

image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 不知道為何,每每遇到心煩氣躁的事情,你就會(huì)來(lái)到我的夢(mèng)里,一起溫習(xí)著舊時(shí)光。 那時(shí)的你笑容滿面,低頭計(jì)算著手...
    小團(tuán)長(zhǎng)閱讀 217評(píng)論 0 0