首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >学习-切换tf-才能注册两个单词的短语

学习-切换tf-才能注册两个单词的短语
EN

Stack Overflow用户
提问于 2022-01-20 03:50:19
回答 1查看 115关注 0票数 1

我正在试验sklearn中的文本分析工具,即这里中的LDA主题提取算法。

我尝试给它其他数据集,在某些情况下,我认为我会得到更好的主题提取结果,如果向量表示的tf-以色列国防军的特征‘可以允许短语。

作为一个简单的例子:

我经常得到一些最重要的词汇联想,比如:

  • 收入
  • net
  • 资产
  • 固定
  • 财宝
  • 财税

这是可以理解的,但我认为我不会得到一个有用的主题提取所需的粒度,除非可以对TfidfVectorizer()或其他参数进行调整,使我得到短语。理想情况下,我想:

  • 固定收益
  • 资产管理
  • 财富管理
  • 净收益
  • 财政收入

为了使事情变得简单,我想象我给算法提供了一个可容忍的2个单词短语的白名单。在对整个语料库中的所有其他词条采用正常的tf-以色列国防军加权时,它只将这些短语计算为独特的词组。

问题

TfidfVectorizer()的文档似乎不支持这一点,但我认为这在实践中是相当普遍的需求--那么实践者是如何做到这一点的呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-20 06:52:25

默认的配置TfidfVectorizer使用ngram_range=(1,1),这意味着它将只使用unigram (单个单词)。

您可以将此参数更改为ngram_range(1,2),以便检索二进制图和二进制图,如果您的二进制图有足够的表示,它们也将被提取。

见下面的例子:

代码语言:javascript
复制
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]
tfidf = TfidfVectorizer(ngram_range=(1,1))
print(f'output with ngram_range=(1,1): {tfidf.fit(corpus).get_feature_names()}\n')

tfidf = TfidfVectorizer(ngram_range=(1,2))
print(f'output with ngram_range=(1,2): {tfidf.fit(corpus).get_feature_names()}')

输出:

代码语言:javascript
复制
output with ngram_range=(1,1): ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']

output with ngram_range=(1,2): ['and', 'and this', 'document', 'document is', 'first', 'first document', 'is', 'is the', 'is this', 'one', 'second', 'second document', 'the', 'the first', 'the second', 'the third', 'third', 'third one', 'this', 'this document', 'this is', 'this the']
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70780593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档