【发布时间】:2016-10-18 18:52:24
【问题描述】:
参考这个post。我想知道我们如何为 CountVectorizer 模型提供带有空格的单词词汇表,例如distributed systems 还是 machine learning?这是一个例子:
import numpy as np
from itertools import chain
tags = [
"python, tools",
"linux, tools, ubuntu",
"distributed systems, linux, networking, tools",
]
vocabulary = list(map(lambda x: x.split(', '), tags))
vocabulary = list(np.unique(list(chain(*vocabulary))))
我们可以将这个词汇表提供给模型
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer(vocabulary=vocabulary)
print(vec.fit_transform(tags).toarray())
在这里,我失去了字数distributed systems(第一列)。结果如下:
[[0 0 0 1 1 0]
[0 1 0 0 1 1]
[0 1 1 0 1 0]]
我是否必须更改 token_pattern 或其他地方?
【问题讨论】:
标签: python machine-learning scikit-learn