Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为scikit学习CountVectorizer提供单词库空间_Python_Machine Learning_Scikit Learn - Fatal编程技术网

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