Calculation of Similarity
@author: Wepon
@blog: http://blog.csdn.net/u012162613/article/details/42213883
"Machine Learning in Action" note, similarity calculation, based on Python+numpy.
In the recommendation system, we need to calculate the similarity of two items, the description of the item can generally be quantified as a vector, so the similarity between the two items can be described by the correlation of two vectors, which is the category of probability theory.
If we want the value of similarity to be between 0~1, and the more similar, the greater the value, what are the options?
1. Calculate the Euclidean distance, then calculate the similarity according to the similarity =1/(1+ distance).
2, Pearson correlation coefficient, in numpy can use linear algebra module Linalg in the Corrcoef () to calculate the correlation coefficient (correlation coefficient). The resulting value range is -1~1, which can be scaled to 0~1 by "0.5+0.5*corrcoef ()".
3, cosine similarity, calculates the cosine of the angle of the two vectors. Cosine value =a*b/(| | a| | *|| b| | ). | | a| | Represents the 2 norm of a, which can be calculated using the norm () in the Linalg module. The cosine value is between -1~1 and also needs to be scaled.
Code:
"" "Created on Sun Dec 10:33:42 2014@author:wepon" "" #相似度计算, InA, inb are line vectors import numpy as Npfrom numpy import Linalg as La #欧式距离def euclidsimilar (INA,INB): return 1.0/(1.0+la.norm (INA-INB)) #皮尔逊相关系数def pearsonsimilar (INA,INB): if Len (InA) <3: return 1.0 return 0.5+0.5*np.corrcoef (ina,inb,rowvar=0) [0][1] #余弦相似度def cossimilar (INA,INB) : Ina=np.mat (INA) Inb=np.mat (InB) num=float (ina*inb.t) denom=la.norm (INA) *la.norm (InB) Return 0.5+0.5* (Num/denom)
Test:
>>> Ina=array ([i]) >>> inb=array ([2,4,6]) >>> euclidsimilar (INA,INB) 0.21089672205953397>>> pearsonsimilar (INA,INB) 1.0>>> cossimilar (InA,inB) 1.0
I feel that these kinds of measurement methods are relatively rough, the final choice of which kind of similarity measure is to see the specific problem it.
Calculation of similarity