tf-idf的原理及计算

17 篇文章 1 订阅
9 篇文章 0 订阅

tf:term frequency

idf;inverse document frequency

TF=某个词在文章中出现的次数/文章的总次数

TF=某个词在文章中出现的次数/该问出现次数最多的词出现的次数

IDF=log(语料库的文档总数/(包含该词的文档数+1))

TF-IDF=TF*IDF

方法1:基于gensim的计算

from gensim.models import TfidfModel
from pprint import pprint
from gensim.corpora import Dictionary

data_set = [["tag1","tag2","tag3"],["tag2","tag2","tag3"],["tag1","tag4","tag3"]]
dct = Dictionary(data_set)
corpus = [dct.doc2bow(line) for line in data_set]
pprint(corpus)
model = TfidfModel(corpus)
model[corpus[0]]

[(0, 0.7071067811865476), (1, 0.7071067811865476)]

方法2:基于scikit-learn的tfidf

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vec = TfidfVectorizer()
# stop words自定义停用词表,为列表List类型
# token_pattern过滤规则,正则表达式,如r"(?u)bw+b
# max_df=0.5,代表一个单词在 50% 的文档中都出现过了,那么它只携带了非常少的信息,因此就不作为分词统计
documents = [
    'this is the bayes document',
    'this is the second second document',
    'and the third one',
    'is this the document'
]
tfidf_matrix = tfidf_vec.fit_transform(documents)
# 拟合模型,并返回文本矩阵  表示了每个单词在每个文档中的 TF-IDF 值
print('输出每个单词在每个文档中的 TF-IDF 值,向量里的顺序是按照词语的 id 顺序来的:', '\n', tfidf_matrix.toarray())
print('不重复的词:', tfidf_vec.get_feature_names())
print('输出每个单词对应的 id 值:', tfidf_vec.vocabulary_)
print('返回idf值:', tfidf_vec.idf_)
print('返回停用词表:', tfidf_vec.stop_words_)

参考:TF-IDF 原理与实现 - 知乎 

TF-IDF介绍及相关代码实现 - 简书 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会发paper的学渣

您的鼓励和将是我前进的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值