如何向scikit-learn的CountVectorizer提供包含空格的词汇

参考这个帖子。我想知道如何向CountVectorizer模型提供包含空格的词汇,例如distributed systemsmachine learning?这是一个例子:

import numpy as npfrom itertools import chaintags = [  "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 CountVectorizervec = 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还是其他地方?


回答:

我认为你已经预定义了要分析的词汇,并且你希望通过分割’, ‘来标记你的标签。

你可以通过以下方式欺骗CountVectorizer来实现这一点:

from sklearn.feature_extraction.text import CountVectorizervec = 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]]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注