코사인 유사도 (Cosine Similarity)

2022. 6. 24. 00:09·Math & Statistics

 

코사인 유사도

의미

코사인 유사도는 두 벡터 간의 코사인 각도를 이용하여 구할 수 있는 두 벡터의 유사도를 의미

두 벡터가 0º의 각을 이루는 경우 (방향이 같을 경우), 코사인 유사도 = 1

두 벡터가 90º의 각을 이루는 경우 (직교할 경우), 코사인 유사도 = 0

두 벡터가 180º의 각을 이루는 경우 (방향이 반대일 경우), 코사인 유사도 = -1

코사인 유사도가 높다는 말은 벡터가 더 비슷하다는 의미

 

 

수학적 표현

벡터의 내적 정의식

$$ \mathbf{A} \cdot \mathbf{B} = \lVert \mathbf{A} \rVert \lVert \mathbf{B} \rVert cos \theta  $$

 

이를 $cos\theta$ 를 중심으로 전개하면

$$ cos\theta = \frac{\mathbf{A} \cdot \mathbf{B}}{\lVert \mathbf{A} \rVert \lVert \mathbf{B} \rVert} $$

 

여기서,

벡터의 내적은 두 벡터의 각 성분끼리의 곱의 합이다

$$ \mathbf{A} \cdot \mathbf{B} = a_1 b_1 + a_2 b_2 + \ldots + a_n b_n = \sum_{i=1}^n a_i b_i $$

L2 Norm은 벡터의 각 성분의 제곱합에 루트를 씌운 것(벡터의 크기)이다

$$ \lVert \mathbf{A} \rVert _2 = \sqrt{a_{1}^{2} + a_{2}^{2} + \ldots + a_{n}^{2}} = \sqrt{\sum_{i=1}^n a_{i}^{2}} $$

 

위의 식을 조합하면 $cos\theta$ 를 구할 수 있는데, 이렇게 구한 cos항을 코사인 유사도라고 한다!

$$ Cosine\ Similarity = cos\theta = \frac{\mathbf{A} \cdot \mathbf{B}}{\lVert \mathbf{A} \rVert \lVert \mathbf{B} \rVert} = \frac{\sum_{i=1}^n a_i b_i}{\sqrt{\sum_{i=1}^n a_{i}^2}\sqrt{\sum_{i=1}^n b_{i}^2}} $$

 

예제

벡터 $\mathbf{a} = (4,2)$와 $\mathbf{b} = (6,3)$의 코사인 유사도를 구하시오.

더보기

$$ \mathbf{a} \cdot \mathbf{b} = (4 \times 6) + (2 \times 3) = 30 $$

$$ \lVert \mathbf{a} \rVert = \sqrt{4^2 + 2^2} = \sqrt{20} $$

$$ \lVert \mathbf{b} \rVert = \sqrt{6^2 + 3^2} = \sqrt{45} $$

$$ cos\theta = \frac{30}{\sqrt{20}\sqrt{45}} = \frac{30}{\sqrt{900}} = 1 $$

 

벡터 $\mathbf{a} = (1,2,3)$와 $\mathbf{b} = (6,5,4)$의 코사인 유사도를 구하시오.

더보기

$$ \mathbf{a} \cdot \mathbf{b} = (1 \times 6) + (2 \times 5) + (3 \times 4) = 28 $$

$$ \lVert \mathbf{a} \rVert = \sqrt{1^2 + 2^2 + 3^2} = \sqrt{14} $$

$$ \lVert \mathbf{b} \rVert = \sqrt{6^2 + 5^2 + 4^2} = \sqrt{77} $$

$$ cos\theta = \frac{28}{\sqrt{14}\sqrt{77}} = \frac{4 \cdot 7}{\sqrt{2 \cdot 7}\sqrt{7 \cdot 11}} = \frac{2 \cdot \sqrt{2} \cdot \sqrt{2} \cdot 7}{\sqrt{2} \cdot \sqrt{7} \cdot \sqrt{7} \cdot \sqrt{11}} = \frac{2\sqrt{2}}{\sqrt{11}} = \frac{2\sqrt{22}}{11} $$

 


Reference

  • <인공지능을 위한 수학> 이시카와 아키히코
  • https://wikidocs.net/24603
  • http://taewan.kim/post/norm/
저작자표시 비영리 변경금지 (새창열림)

'Math & Statistics' 카테고리의 다른 글

매크로 평균(Macro-Average)과 마이크로 평균(Micro-Average)  (0) 2023.01.03
Bayes' theorem 베이즈 정리  (0) 2022.06.07
Markov Chains, 마르코프 체인  (0) 2022.05.21
Taylor series, 테일러 급수  (0) 2022.05.13
확률과 통계 개념정리  (0) 2022.03.12
'Math & Statistics' 카테고리의 다른 글
  • 매크로 평균(Macro-Average)과 마이크로 평균(Micro-Average)
  • Bayes' theorem 베이즈 정리
  • Markov Chains, 마르코프 체인
  • Taylor series, 테일러 급수
카이로셜
카이로셜
  • 카이로셜
    카이로스의 시간
    카이로셜
  • 글쓰기 관리
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Programming
        • Python
        • Linux
        • Git, Github
        • ML, Machine Learning
        • DL, Deep Learning
        • NLP
        • Computer Vision
        • Network
        • PyCharm
      • IT
        • Windows
        • Mac OS
        • Programs
        • 한글
        • Word
        • Excel
        • PowerPoint
      • Math & Statistics
      • English
      • Graduate School
      • etc.
      • Record
  • 블로그 메뉴

    • Github
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    아나콘다
    객체
    docker
    도커
    윈도우10
    아나콘다 가상환경
    윈도우11
    클래스
    맥북 단축키
    anaconda
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
카이로셜
코사인 유사도 (Cosine Similarity)
상단으로

티스토리툴바