tf-idf 原理及实践

TF(Term Frequency,缩写为TF)

也就是词频啦,即一个词在文中出现的次数

在这里插入图片描述

逆文档频率"(IDF):

在这里插入图片描述
如果一个词越常见,那么分母就越大,逆文档频率就越小越接近0。分母之所以要加1,是为了避免分母为0(即所有文档都不包含该词)。log表示对得到的值取对

用统计学语言表达,就是在词频的基础上,要对每个词分配一个"重要性"权重 ,这个词越常见 给予较小的权重,较少见的词 给予较大的权重 ; 个人理解 在每个文档都能出现的词 对这个文档的主题 贡献越弱 这是比较合理的。

TF-IDF

TF-IDF = TF * IDF

可以看到,TF-IDF与一个词在文档中的出现次数成正比,与该词在整个语言中的出现次数成反比。所以,自动提取关键词的算法就很清楚了,就是计算出文档的每个词的TF-IDF值,然后按降序排列,取排在最前面的几个词。

代码实现

注释已经加入代码 要安装 gensim 包
得到每个词 在每个文章中的 tfidf 为后面计算 文章的主题等任务 做准备


from gensim import corpora, models, similarities

#将所有的语料 放入一个list中 用逗号隔开  每一个逗号 表示一篇文章
documents = ["Human machine interface for lab abc computer applications",
             "A survey of user opinion of computer system response time",
             "The EPS user interface 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)

# 词典 将文章里面所有的词 按照 顺序  和 词对应起来
#dict= (0, 'abc')(1, 'applications')(2, 'computer')(3, 'for')

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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值