Calculation of similarity

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.