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