我已經看過使用Python計算文檔中單詞TF-IDF分數的幾種方法。我選擇使用TextBlob。Python(TextBlob)TF-IDF計算
我得到一個輸出,但是,它們是負值。我知道這是不正確的(非負數量(tf)除以正數(df)(的對數)的log(log)不會產生負數值)。
我看過這裏發佈的以下問題:TFIDF calculating confusion但它沒有幫助。
我是如何在計算分數:
def tf(word, blob):
return blob.words.count(word)/len(blob.words)
def n_containing(word, bloblist):
return sum(1 for blob in bloblist if word in blob)
def idf(word, bloblist):
return math.log(len(bloblist)/(1 + n_containing(word, bloblist)))
def tfidf(word, blob, bloblist):
return tf(word, blob) * idf(word, bloblist)
然後我簡單地打印出他們的成績的話。
"hello, this is a test. a test is always good."
Top words in document
Word: good, TF-IDF: -0.06931
Word: this, TF-IDF: -0.06931
Word: always, TF-IDF: -0.06931
Word: hello, TF-IDF: -0.06931
Word: a, TF-IDF: -0.13863
Word: is, TF-IDF: -0.13863
Word: test, TF-IDF: -0.13863
與小知識,我有什麼,我所看到的,它可能是以色列國防軍計算不正確?
所有幫助將不勝感激。感謝
日誌x的如果0
yurib
@yurib值不能是負的,因爲它們在文檔中存在... – user47467
我同意tfidf評分不應該是負面的,我在技術上指出,你的實施可以返回一個負面結果。例如,如果一個單詞出現在所有blob中,則idf()將返回log(len(bloblist)/(len(bloblist)+1)),這將是負面的。 – yurib