정의: 두 벡터가 가리키는 방향이 얼마나 비슷한지 측정
값 범위: -1 ~ 1
문서 유사도 계산 시: 문서 → 벡터로 변환(DTM, TF-IDF 등) → 벡터 간 각도 계산
수식
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
💡 포인트
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))
📌 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. ]]
you
)의 영향이 줄었기 때문💡 정리