Python 为scikit学习CountVectorizer提供单词库空间
关于这一点。我想知道我们如何为Python 为scikit学习CountVectorizer提供单词库空间,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,关于这一点。我想知道我们如何为CountVectorizer模型提供单词词汇空间,例如分布式系统或机器学习?以下是一个例子: import numpy as np from itertools import chain tags = [ "python, tools", "linux, tools, ubuntu", "distributed systems, linux, networking, tools", ] vocabulary = list(map(lambda x:
CountVectorizer
模型提供单词词汇空间,例如分布式系统
或机器学习
?以下是一个例子:
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())
这里,我忘记了word分布式系统的计数(第一列)。结果如下:
[[0 0 0 1 1 0]
[0 1 0 0 1 1]
[0 1 1 0 1 0]]
我是否必须更改token\u模式
或其他地方?我认为基本上您已经预定义了要分析的词汇表,并且您希望通过拆分“,”来标记您的标记
您可以通过以下方式欺骗计数向量器
:
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer(vocabulary=vocabulary, tokenizer=lambda x: x.split(', '))
print(vec.fit_transform(tags).toarray())
,其中:
[[0 0 0 1 1 0]
[0 1 0 0 1 1]
[1 1 1 0 1 0]]
非常感谢@Zichen,这就是我要找的。使用tokenizer