tf idf 原理及實踐

2021-10-04 07:21:37 字數 1903 閱讀 9283

也就是詞頻啦,即乙個詞在文**現的次數

如果乙個詞越常見,那麼分母就越大,逆文件頻率就越小越接近0。分母之所以要加1,是為了避免分母為0(即所有文件都不包含該詞)。log表示對得到的值取對

用統計學語言表達,就是在詞頻的基礎上,要對每個詞分配乙個"重要性"權重 ,這個詞越常見 給予較小的權重,較少見的詞 給予較大的權重 ; 個人理解 在每個文件都能出現的詞 對這個文件的主題 貢獻越弱 這是比較合理的。

tf-idf = tf * idf

注釋已經加入** 要安裝 gensim 包

得到每個詞 在每個文章中的 tfidf 為後面計算 文章的主題等任務 做準備

from gensim import corpora, models, similarities

#將所有的語料 放入乙個list中 用逗號隔開 每乙個逗號 表示一篇文章

documents =[,

"a survey of user opinion of computer system response time"

,"the eps user inte***ce management system"

,"system and human system engineering testing of eps"

,"relation of user perceived response time to error measurement"

,"the generation of random binary unordered trees"

,"the intersection graph of paths in trees"

,"graph minors iv widths of trees and well quasi ordering"

,"graph minors a survey"

]#切割文章 變成 list 形式 [ [ 單詞1, 單詞2], [ 單詞3 ,單詞4]]

texts =

[[word for word in document.lower(

).split()]

for document in documents]

print

("texts=="

,texts)

# 詞典 將文章裡面所有的詞 按照 順序 和 詞對應起來

dictionary = corpora.dictionary(texts)

for k in dictionary.iteritems():

print

("dict="

,k)# 詞庫,以(詞,詞頻)方式存貯

#(0, 1), (1, 1), (2, 1), (3, 1)

corpus =

[dictionary.doc2bow(text)

for text in texts]

print

("corpus=="

,corpus)

#初始化語料

tfidf = models.tfidfmodel(corpus)

#計算每個詞的 tf-idf

corpus_tfidf = tfidf[corpus]

# (0, 0.39510679503439006), (1, 0.39510679503439006), (2, 0.270464478621662),

for doc in corpus_tfidf:

print

("doc"

,doc)

TF IDF原理與實踐

在資訊檢索中,tf idf 詞頻 逆文件頻率 是一種統計方法,用以評估乙個單詞在乙個文件集合或語料庫中的重要程度。經常被用作資訊檢索 文字挖掘以及使用者模型的權重因素。tf idf的值會隨著單詞在文件 現的次數的增加而增大,也會隨著單詞在語料庫 現的次數的增多而減小。tf idf是如今最流行的詞頻加...

輕鬆理解TF IDF原理及應用

在了解tf idf原理前,我們首先需要高清楚為啥需要它以及它能解決什麼問題?下面我們先從以計數為特徵的文字向量化來說起。計數特徵,簡單來講就是統計每個特徵詞在文件中出現的次數,把次數作為特徵的權重。因此在以計數特徵文字分詞並向量化後,我們可以得到詞彙表中每個詞在各個文字中形成的詞向量,比如我們將下面...

TFIDF演算法原理

概念 tf idf term frequency inverse document frequency 是一種用於資訊檢索與資訊探勘的常用加權技術。tf idf是一種統計方法,用以評估一字詞對於乙個檔案集或乙個語料庫中的其中乙份檔案的重要程度。字詞的重要性隨著它在檔案中出現的次數成正比增加,但同時會...