코사인 유사도를 이용한 추천 시스템

허허맨·2025년 8월 1일
0

LLM

목록 보기
9/12
post-thumbnail

📌 코사인 유사도를 이용한 추천 시스템

1. 코사인 유사도란?

  • 정의: 두 벡터가 가리키는 방향이 얼마나 비슷한지 측정

  • 값 범위: -1 ~ 1

    • 1 → 완전히 같은 방향 (유사도 최대)
    • 0 → 90° (서로 완전 무관)
    • -1 → 완전히 반대 방향
  • 문서 유사도 계산 시: 문서 → 벡터로 변환(DTM, TF-IDF 등) → 벡터 간 각도 계산

수식

cos_sim(A,B)=ABAB\text{cos\_sim}(A, B) = \frac{A \cdot B}{||A|| \cdot ||B||}

2. 간단 예시

import numpy as np
from numpy import dot
from numpy.linalg import norm

def cos_sim(A, B):
    return dot(A, B) / (norm(A) * norm(B))

doc1 = np.array([0,1,1,1])
doc2 = np.array([1,0,1,1])
doc3 = np.array([2,0,2,2])

print("문서1 vs 문서2:", cos_sim(doc1, doc2))
print("문서1 vs 문서3:", cos_sim(doc1, doc3))
print("문서2 vs 문서3:", cos_sim(doc2, doc3))

출력

문서1 vs 문서2: 0.67
문서1 vs 문서3: 0.67
문서2 vs 문서3: 1.00

💡 포인트

  • 문서2와 문서3은 단어 비율이 완전히 동일 → 코사인 유사도 1
  • 문서 길이 차이가 나더라도 방향(패턴)이 같으면 1이 됨 → 문서 길이에 영향 없음

3. DTM & TF-IDF + 코사인 유사도 예제

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import pandas as pd

# 문서 데이터
corpus = [
    'you know I want your love',
    'I like you',
    'what should I do'
]

# (1) DTM
vector = CountVectorizer()
dtm = vector.fit_transform(corpus).toarray()
terms = vector.get_feature_names_out()
df_dtm = pd.DataFrame(dtm, columns=terms)

print("📌 DTM\n", df_dtm)

# (2) TF-IDF
tfidfv = TfidfVectorizer()
tfidf = tfidfv.fit_transform(corpus).toarray()
terms_tfidf = tfidfv.get_feature_names_out()
df_tfidf = pd.DataFrame(tfidf, columns=terms_tfidf)

print("\n📌 TF-IDF\n", df_tfidf.round(3))

# (3) 코사인 유사도 계산
cos_sim_dtm = cosine_similarity(dtm, dtm)
cos_sim_tfidf = cosine_similarity(tfidf, tfidf)

print("\n📌 코사인 유사도 (DTM 기반)\n", cos_sim_dtm.round(3))
print("\n📌 코사인 유사도 (TF-IDF 기반)\n", cos_sim_tfidf.round(3))

4. 실행 결과 예시

📌 DTM
   do  know  like  love  should  want  what  you  your
0   0     1     0     1       0     1     0    1     1
1   0     0     1     0       0     0     0    1     0
2   1     0     0     0       1     0     1    0     0

📌 TF-IDF
     do   know   like   love  should   want   what    you   your
0  0.000  0.467  0.000  0.467  0.000  0.467  0.000  0.355  0.467
1  0.000  0.000  0.796  0.000  0.000  0.000  0.000  0.605  0.000
2  0.577  0.000  0.000  0.000  0.577  0.000  0.577  0.000  0.000

📌 코사인 유사도 (DTM 기반)
[[1.    0.25  0.25 ]
 [0.25  1.    0.333]
 [0.25  0.333 1.   ]]

📌 코사인 유사도 (TF-IDF 기반)
[[1.    0.271 0.298]
 [0.271 1.    0.345]
 [0.298 0.345 1.   ]]

5. 해석

  • DTM 기반 코사인 유사도: 단순 단어 빈도 패턴 비교
  • TF-IDF 기반 코사인 유사도: 불필요 단어 영향 줄이고 핵심 단어 중심으로 비교
  • 예) 문서1과 문서2의 TF-IDF 유사도가 DTM보다 낮음 → 자주 쓰이는 공통 단어(you)의 영향이 줄었기 때문

💡 정리

  1. DTM: 단어 빈도 기반 문서 비교
  2. TF-IDF: 단어 중요도 반영 → 불용어 영향 최소화
  3. 코사인 유사도: 문서 길이 영향 없이 패턴 유사도 측정 가능
    → 추천 시스템, 검색엔진, 문서 클러스터링 등에 필수

profile
사람은 망각의 동물입니다. 때로는 기록으로 과거의 나를 데려옵니다.

0개의 댓글