2014-03-28 5 views
0

:코사인 유사성 [파이썬] 데이터와 쿼리의 코사인 유사도를 계산 내 함수의 다음 코드로

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이 : 그래서 enter image description here

을 할 수 제 코드를 도와 주시겠습니까? 잘못된 값을 반환하고 이유를 모르겠습니다. 감사합니다. . 문서 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 

답변

0

당신이 빨간색 용어를 포함하거나하지 않았다? 참조 솔루션에 포함 되었습니까, 포함되지 않았습니까?

그들은 순위 같은 을 획득하지만 난 틀리지 않는 경우가의 숫자 값에 영향을해야합니다에 대한 필요하지 않습니다.

또한 코사인 유사성 정의는 1+log 조항의 사용으로 인해 비표준으로 보입니다.

https://en.wikipedia.org/wiki/Cosine_similarity

+0

기준 용액 즉, 문서 LTC이고 DT하고 상기 QT 대한 IDF없이 적색 부분, 쿼리 LNN 코사인 점수를 산출 ltc.lnn 가중치를 사용 (dt의 경우 tfidf이고 쿼리의 경우 1 + log (tf) 만). 나는 그것을 정확하게하고 있다고 생각하지만 결과를 얻지는 못한다 :/ – SerCrAsH

+0

'len (query)'는 적어도 N/df가 아니다; 당신의'qt' 용어가 부정확 할 것입니다. 그러나 대부분은 빠져 있기 때문에 코드를 실제로 확인할 수는 없습니다 (예 :'get_tfidf'). –

0
def calctfidfvec(tfvec, withidf): 
    tfidfvec = {} 
    veclen = 0.0 

    for token in tfvec: 
     if withidf: 
      tfidf = (1+log10(tfvec[token])) * getidf(token) 
     else: 
      tfidf = (1+log10(tfvec[token])) 
     tfidfvec[token] = tfidf 
     veclen += pow(tfidf,2) 

    if veclen > 0: 
     for token in tfvec: 
      tfidfvec[token] /= sqrt(veclen) 

    return tfidfvec 

def cosinesim(vec1, vec2): 
    commonterms = set(vec1).intersection(vec2) 
    sim = 0.0 
    for token in commonterms: 
     sim += vec1[token]*vec2[token] 

    return sim 
+0

설명을 추가해주세요. –