我知道在 Python 的 Scikit-learn 包中创建计数矢量化器时可以指定最小频率。但是,我想知道是否只有在您不提供先验字典的情况下才会出现这种情况?当我提供自己的自定义词汇表(列表)时,此参数似乎不起作用。
为了解决这个问题,我重新阅读了关于 min_df 参数的文档:
Parameters :
min_df : float in range [0.0, 1.0] or int, optional, 1 by default
When building the vocabulary, ignore terms that have a term frequency strictly lower than the given threshold.
This value is also called cut-off in the literature.
If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None.
在我的例子中,我向我的 CountVectorizer 提供了一个自定义词汇表,该词汇表由我之前获得的自己的术语组成。
cv = CountVectorizer(vocabulary=my_own_terms, min_df=3)
X = cv.fit_transform(a_big_corpus)
当我查看输出时,我得到了出现一次、两次等的各种术语。
有没有人在他们的工作中发生过这种情况,如果有,是否有可能的解决方案?提前致谢。
请您参考如下方法:
通过提供词汇集,这意味着您正是需要该词汇集。其他任何事情都无关紧要,即使一个术语只出现一次或根本没有出现在要转换的文本中。否则,它与您想要正是那组词汇的隐含意图(通过提供 vocabulary 参数)相矛盾。因此,根据文档,忽略 min_df 约束似乎是合理的。
根据您的后续评论,您似乎想要的是:
I want 'exactly' those items in my vocabulary, but if a word occurs occurs only once, then I don't want it.
这基本上意味着您希望预先提取的词汇集中的术语出现得足够频繁。换句话说,你实际上并不是真的想要那些词汇。我可能会这样做:
- 从头开始为您的数据安装一个全新的
CounterVectorizer。 - 将上面新安装的向量化器的词汇与您的先验词汇集相交。
- 构建实际的
CountVectorizer,它使用在第 2 步中交叉的词汇表进行真正的转换。
您的方法直接跳到第 3 步,这就是矢量化器无法按预期工作的原因。
