ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 유사도 구하기 (피어슨 상관계수)
    machine learning 2019. 3. 13. 19:16

    피어슨 상관계수(Pearson correlation coefficient)

    Mathematical Expression

    유사도를 구하는 다른 방법중엔유클리디안, 코사인, 맨하튼 거리, 타니모토 계수등이 있다.
    피어슨 상관계수는 두 변수의 선형관계를 파악하기 위해 사용하며 결과값 r은 (X와 Y가 함께 변하는 정도) / (X와 Y가 따로 변하는 정도) 이다.

    Code

    이 예제는 programming collective intelligence에 나오는 pearsson 함수입니다.

    def sim_pearson(prefs,p1,p2):
    # Get the list of mutually rated items
    si={}
    for item in prefs[p1]:
    if item in prefs[p2]: si[item]=1
    # if they are no ratings in common, return 0
    if len(si)==0: return 0
    # Sum calculations
    n=len(si)
    # Sums of all the preferences
    sum1=sum([prefs[p1][it] for it in si])
    sum2=sum([prefs[p2][it] for it in si])
    # Sums of the squares
    sum1Sq=sum([pow(prefs[p1][it],2) for it in si])
    sum2Sq=sum([pow(prefs[p2][it],2) for it in si])
    # Sum of the products
    pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si])
    # Calculate r (Pearson score)
    num=pSum-(sum1*sum2/n)
    den=sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n))
    if den==0: return 0
    r=num/den
    return r

    Result

    r 값은 X 와 Y 가 완전히 동일하면 +1, 전혀 다르면 0, 반대방향으로 완전히 동일 하면 –1 을 가진다. 결정계수 (coefficient of determination) 는 r² 로 계산하며 이것은 X 로부터 Y 를 예측할 수 있는 정도를 의미한다.

    r이 -1.0과 -0.7 사이이면, 강한 음적 선형관계
    r이 -0.7과 -0.3 사이이면, 뚜렷한 음적 선형관계
    r이 -0.3과 -0.1 사이이면, 약한 음적 선형관계
    r이 -0.1과 +0.1 사이이면, 거의 무시될 수 있는 선형관계
    r이 +0.1과 +0.3 사이이면, 약한 양적 선형관계
    r이 +0.3과 +0.7 사이이면, 뚜렷한 양적 선형관계
    r이 +0.7과 +1.0 사이이면, 강한 양적 선형관계


    반응형

    'machine learning' 카테고리의 다른 글

    Bias-variance tradeoff  (0) 2020.01.24
    softmax / cross-entropy / kld  (0) 2019.07.30
    K-Nearest Neighbor(KNN)  (0) 2019.03.13
    타니모토 계수(Tanimoto coefficient)  (0) 2019.03.13

    댓글

Designed by Tistory.