余弦相似度[Python]

时间:2014-03-28 22:21:52

标签: python python-2.7 data-mining cosine-similarity

使用以下函数代码计算查询与数据的余弦相似度:

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

我只有下一个故事: enter image description here

那么,你能帮我解决一下我的代码吗?我回复了错误的价值观,我不知道为什么。 感谢。

doc 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

2 个答案:

答案 0 :(得分:0)

您是否包含红色条款?参考解决方案是否包括它们?

对于获得相同的排名,它们不是必需的,但如果我没有弄错的话,它们应该对数值值产生影响。

此外,由于使用了这些1+log术语,您对余弦相似度的定义似乎是非标准的。

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

答案 1 :(得分: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