[机器学习]TF-IDF是什么

一,前言

在信息检索与文本挖掘中经常遇见单词的 tf-idf (term frequency - inverse document frequency),这个值的大小能够体现它在文本集合中的某一个文档里的重要性。

TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度。字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降。TF-IDF加权的各种形式常被搜索引擎应用,作为文件与用户查询之间相关程度的度量或评级。除了TF-IDF以外,因特网上的搜索引擎还会使用基于链接分析的评级方法,以确定文件在搜寻结果中出现的顺序。

二,什么是TF-IDF

举个例子来说,有一篇100字的短文,其中「猫」这个词出现了3 次。那么这篇短文中「猫」的词频
在这里插入图片描述
如果这里有 10000000 篇文章,其中有「猫」这个词的却文章只有 1000个,那么「猫」对应所有文本,也就是整个语料库的逆向文件频率
在这里插入图片描述
这里 l o g log log取 10为底。 这样就可以计算得到「猫」在这篇文章中的
在这里插入图片描述
现在假设在同一篇文章中,「是」这个词出现了20次,因此「是」这个字的词频为0.2。如果只计算词频的话,在这篇文章中明显「是」是比「猫」重要的。

但我们还有逆向文件频率,了解到「是」这个字在全部的 10000000 篇文章都出现过了(这样假设可以吗?),那么「是」的逆向文件頻率就是
在这里插入图片描述
这样综合下来,「是」这个字的 tf-idf 就只有 0了,远不及「猫」重要。

这样在计算 tf-idf 就可以知道,对于这篇文章,「猫」这个词远比出现更多次的「是」重要。诸如此类出现很多次,但实际上并不包含文章特征信息的词还有很多,比如「这」,「也」,「就」,「是」,「的」,「了」。

那么关于 tf-idf 的解释,这也就是的了。

三,TfidfVectorizer与CountVectorizer的代码

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import numpy as np
def test_CountVectorizer():

    # 1. give a simple dataset
    simple_train = ["don't call you tonight", "Call me isn't a cab", 'please call me PLEASE!']
    # 2. to conduct a CountVectorizer object
    cv = CountVectorizer()
    #cv = CountVectorizer()
    cv.fit(simple_train)
    # to print the vocabulary of the simple_train
    print(cv.vocabulary_)
    # 3. 4.transform training data into a 'document-term matrix' (which is a sparse matrix) use “transform()”
    train_data = cv.transform(simple_train)
    # (the index of the list , the index of the dict ) the frequency of the list[index]
    print(cv.get_feature_names())
    print(train_data)

    train_data = train_data.toarray()
    print(train_data)

    # 7. transform testing data into a document-term matrix (using existing vocabulary)
    simple_test = ["please don't call me"]
    test_data = cv.transform(simple_test).toarray()
    # 8. examine the vocabulary and document-term matrix together
    print(test_data)

def test_tfidf_filter_vec():
    simple_train = ['call you tonight', 'Call me a cab', 'please call me... PLEASE!']
    cv = TfidfVectorizer()
    cv.fit(simple_train)
    print(cv.vocabulary_)
    train_data = cv.transform(simple_train)
    print(cv.get_feature_names())
    print(train_data)

    train_data = train_data.toarray()
    print(train_data)


if __name__ == '__main__':
    #test_CountVectorizer()
    test_tfidf_filter_vec()

test_tfidf_filter_vec方法执行的结果

{'call': 1, 'you': 5, 'tonight': 4, 'me': 2, 'cab': 0, 'please': 3}
['cab', 'call', 'me', 'please', 'tonight', 'you']
  (0, 5)	0.652490884512534
  (0, 4)	0.652490884512534
  (0, 1)	0.3853716274664007
  (1, 2)	0.5478321549274363
  (1, 1)	0.4254405389711991
  (1, 0)	0.7203334490549893
  (2, 3)	0.901008145286396
  (2, 2)	0.3426199591918006
  (2, 1)	0.2660749625405929
[[0.         0.38537163 0.         0.         0.65249088 0.65249088]
 [0.72033345 0.42544054 0.54783215 0.         0.         0.        ]
 [0.         0.26607496 0.34261996 0.90100815 0.         0.        ]]

Process finished with exit code 0

test_CountVectorizer方法执行的结果

{'don': 2, 'call': 1, 'you': 7, 'tonight': 6, 'me': 4, 'isn': 3, 'cab': 0, 'please': 5}
['cab', 'call', 'don', 'isn', 'me', 'please', 'tonight', 'you']
  (0, 1)	1
  (0, 2)	1
  (0, 6)	1
  (0, 7)	1
  (1, 0)	1
  (1, 1)	1
  (1, 3)	1
  (1, 4)	1
  (2, 1)	1
  (2, 4)	1
  (2, 5)	2
[[0 1 1 0 0 0 1 1]
 [1 1 0 1 1 0 0 0]
 [0 1 0 0 1 2 0 0]]
[[0 1 1 0 1 1 0 0]]

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

茫茫人海一粒沙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值