我正在试验sklearn中的文本分析工具,即这里中的LDA主题提取算法。
我尝试给它其他数据集,在某些情况下,我认为我会得到更好的主题提取结果,如果向量表示的tf-以色列国防军的特征‘可以允许短语。
作为一个简单的例子:
我经常得到一些最重要的词汇联想,比如:
这是可以理解的,但我认为我不会得到一个有用的主题提取所需的粒度,除非可以对TfidfVectorizer()或其他参数进行调整,使我得到短语。理想情况下,我想:
为了使事情变得简单,我想象我给算法提供了一个可容忍的2个单词短语的白名单。在对整个语料库中的所有其他词条采用正常的tf-以色列国防军加权时,它只将这些短语计算为独特的词组。
问题
TfidfVectorizer()的文档似乎不支持这一点,但我认为这在实践中是相当普遍的需求--那么实践者是如何做到这一点的呢?
发布于 2022-01-20 06:52:25
默认的配置TfidfVectorizer使用ngram_range=(1,1),这意味着它将只使用unigram (单个单词)。
您可以将此参数更改为ngram_range(1,2),以便检索二进制图和二进制图,如果您的二进制图有足够的表示,它们也将被提取。
见下面的例子:
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()}')输出:
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']https://stackoverflow.com/questions/70780593
复制相似问题