TF-IDF 概念+代码实现

TF-IDF

TF-IDF stands for “Term Frequency — Inverse Data Frequency”.

To put it in more formal mathematical terms, the TF-IDF score for the word t in the document d from the document set D is calculated as follows:

t f i d f ( t , d , D ) = t f ( t , d ) . i d f ( t , D ) t f i d f(t, d, D)=t f(t, d) . i d f(t, D) tfidf(t,d,D)=tf(t,d).idf(t,D)

Term Frequency (tf): gives us the frequency of the word in each document in the corpus. It is the ratio of number of times the word appears in a document compared to the total number of words in that document. It increases as the number of occurrences of that word within the document increases. Each document has its own tf.

t f ( t , d ) = log ⁡ ( 1 + freq ⁡ ( t , d ) ) t f(t, d)=\log (1+\operatorname{freq}(t, d)) tf(t,d)=log(1+freq(t,d))

Inverse Data Frequency (idf): used to calculate the weight of rare words across all documents in the corpus. The words that occur rarely in the corpus have a high IDF score. It is given by the equation below.(N is total number of documents.)

idf ⁡ ( t , D ) = log ⁡ ( N count ⁡ ( d ∈ D : t ∈ d ) ) \operatorname{idf}(t, D)=\log \left(\frac{N}{\operatorname{count}(d \in D: t \in d)}\right) idf(t,D)=log(count(dD:td)N)

Let’s take an example to get a clearer understanding.

Sentence 1 : The car is driven on the road.
Sentence 2: The truck is driven on the highway.

In this example, each sentence is a separate document.We will now calculate the TF-IDF for the above two documents, which represent our corpus.

在这里插入图片描述

From the above table, we can see that TF-IDF of common words was zero, which shows they are not significant. On the other hand, the TF-IDF of “car” , “truck”, “road”, and “highway” are non-zero. These words have more significance.

Implementing TF-IDF in Python

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd

train = ['The sky is blue.','The sun is bright.']

tfidfvectorizer = TfidfVectorizer(analyzer='word',stop_words= 'english')
countvectorizer = CountVectorizer(analyzer= 'word', stop_words='english')
tfidf_wm = tfidfvectorizer.fit_transform(train)
tfidf_tokens = tfidfvectorizer.get_feature_names()
df_tfidfvect = pd.DataFrame(data = tfidf_wm.toarray(),index = ['Doc1','Doc2'],columns = tfidf_tokens)
weight=tfidf_wm.toarray()

for i in range(len(weight)):#打印每类文本的tf-idf词语权重,第一个for遍历所有文本,第二个for便利某一类文本下的词语权重  
    print ("-------这里输出第",i,u"类文本的词语tf-idf权重------" )
    for j in range(len(tfidf_tokens)):  
        print(tfidf_tokens[j],weight[i][j])
# output
-------这里输出第 0 类文本的词语tf-idf权重------
blue 0.7071067811865476
bright 0.0
sky 0.7071067811865476
sun 0.0
-------这里输出第 1 类文本的词语tf-idf权重------
blue 0.0
bright 0.7071067811865476
sky 0.0
sun 0.7071067811865476

References

  1. How to process textual data using TF-IDF in Python
  2. TF-IDF Vectorizer scikit-learn
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hUaleeF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值