:코사인 유사성 [파이썬] 데이터와 쿼리의 코사인 유사도를 계산 내 함수의 다음 코드로
def rank_retrieve(self, query):
"""
Given a query (a list of words), return a rank-ordered list of
documents and score for the query.
self.docs : list of documents
self.docs[i] : list of words in doc number i -> [word1,word2,...,wordN]
self.boolean_retrieve(query) : giving a list of words this return the index of
documents wich contains all of these words.
self.tfidf(word,documentIndex) : returns the value tfidf of a word in a document
self.get_posting(word): returns a list of document index where that word appears
"""
scores = [0.0 for xx in range(len(self.docs))]
# Apply Cosine Similarity
for i in self.boolean_retrieve(query):
normDoc = 0.0
normQuery = 0.0
dt = 0.0
qtdt = 0.0
for word in query:
dt = self.get_tfidf(word,i)
normDoc+= math.pow(dt,2)
qt = 1.0 + (math.log10(len(query)))
normQuery+=math.pow(qt,2)
qtdt += dt * qt
scores[i] = qtdt/(math.sqrt(normDoc))
return scores
난 그냥 다음 teory이 : 그래서
을 할 수 제 코드를 도와 주시겠습니까? 잘못된 값을 반환하고 이유를 모르겠습니다. 감사합니다. . 문서 56 점의
결과 :
Cosine Similarity Test
doc 56, query : ['separ', 'of', 'church', 'and', 'state']
Separ:
QTDT: 0.105587429399
DT 0.0621479067488
QT 1.69897000434
NormDoc: 0.00386236231326 normQuery 2.88649907563
Of:
QTDT: 0.105587429399
DT 0.0
QT 1.69897000434
NormDoc: 0.00386236231326 normQuery 5.77299815127
Church :
QTDT: 0.653857934128
DT 0.322707583613
QT 1.69897000434
NormDoc: 0.108002546834 normQuery 8.6594972269
And:
QTDT: 0.653857934128
DT 0.0
QT 1.69897000434
NormDoc: 0.108002546834 normQuery 11.5459963025
State:
QTDT: 0.674927180008
DT 0.0124011876763
QT 1.69897000434
NormDoc: 0.10815633629 normQuery 14.4324953782
Scores of 56 must be 0.010676611271744128 found : 2.05225316563
기준 용액 즉, 문서 LTC이고 DT하고 상기 QT 대한 IDF없이 적색 부분, 쿼리 LNN 코사인 점수를 산출 ltc.lnn 가중치를 사용 (dt의 경우 tfidf이고 쿼리의 경우 1 + log (tf) 만). 나는 그것을 정확하게하고 있다고 생각하지만 결과를 얻지는 못한다 :/ – SerCrAsH
'len (query)'는 적어도 N/df가 아니다; 당신의'qt' 용어가 부정확 할 것입니다. 그러나 대부분은 빠져 있기 때문에 코드를 실제로 확인할 수는 없습니다 (예 :'get_tfidf'). –