使用CountVectorizer为LDA主题模型准备数据集

时间:2018-04-08 07:15:40

标签: python scikit-learn lda topic-modeling countvectorizer

我想使用CountVectorizer中的Scikit创建一个由LDA模型使用的矩阵。但我的数据集是一系列编码术语,例如以下列形式:

(1-2252, 5-5588, 10-5478, 2-9632 ....)

如何告诉CountVectorizer将每对数据(1-2252)视为一个单词

1 个答案:

答案 0 :(得分:0)

幸运的是,我找到了一个helpful博客给了我答案。

我使用以下方法来标记文本:

import re
REGEX = re.compile(r",\s*")
def tokenize(text):
    return [tok.strip().lower() for tok in REGEX.split(text)]

将标记器传递给CountVectorizer,如下所示:

tf = CountVectorizer(tokenizer=tokenize)