在Python中使用while函数将短语更改为向量

2024-05-13 17:21:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用sklearn将以下短语改为vectors:

Article 1. It is not good to eat pizza after midnight
Article 2. I wouldn't survive a day withouth stackexchange
Article 3. All of these are just random phrases
Article 4. To prove if my experiment works.
Article 5. The red dog jumps over the lazy fox

我得到了以下代码:

from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(min_df=1)

n=0
while n < 5:
   n = n + 1
   a = ('Article %(number)s' % {'number': n})
   print(a)
   with open("LISR2.txt") as openfile:
     for line in openfile:
       if a in line:
           X=line
           print(vectorizer.fit_transform(X))

这给了我以下错误:

ValueError: Iterable over raw text documents expected, string object received.

为什么会这样?我知道这应该有效,因为如果我单独输入:

X=("It is not good to eat pizza","I wouldn't survive a day", "All of these")

print(vectorizer.fit_transform(X))

它给了我想要的向量。

(0, 8)  1
(0, 2)  1
(0, 11) 1
(0, 3)  1
(0, 6)  1
(0, 4)  1
(0, 5)  1
(1, 1)  1
(1, 9)  1
(1, 12) 1
(2, 10) 1
(2, 7)  1
(2, 0)  1

Tags: toislinearticlenotitsklearngood
2条回答

当您提供原始数据时会出现这个问题,这意味着直接将字符串提供给提取函数,而您可以提供Y=[X]并将这个Y作为参数传递,然后您将得到正确的结果我也遇到了这个问题

看看the docs。它说CountVectorizer.fit_transform需要一个iterable字符串(例如,字符串列表)。而是传递一个字符串。

这很有意义,scikit中的fit_转换做两件事:1)它学习一个模型(fit)2)它将模型应用于数据(transform)。您需要构建一个矩阵,其中列是词汇表中的所有单词,行对应于文档。为此,你需要知道你的语料库中的全部词汇(所有的列)。

相关问题 更多 >