Python 可迭代原始文本文档,收到字符串对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49806790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterable over raw text documents expected, string object received
提问by thegreatcoder
I am currently trying to build a naive Bayes classifier as mentioned in this link.Referring to the line
我目前正在尝试构建一个本链接中提到的朴素贝叶斯分类器。参考线
X_new_tfidf = tfidf_transformer.transform(X_new_counts)
under the Training the Classifier
subheading, I had a similar line, X_new_counts = count_vect.transform(input.plot_movie)
in my code which should take an iterable as an input to the transform function. The input
is a record from a DataFrame and is of type pd.Series
and contains the following entries, out of which I send input.plot_movie
as the input to the transform function:
在Training the Classifier
副标题下,X_new_counts = count_vect.transform(input.plot_movie)
我的代码中有一条类似的行,它应该将一个可迭代对象作为转换函数的输入。这 input
是来自 DataFrame 的记录,属于类型pd.Series
并包含以下条目,我将其中的条目input.plot_movie
作为输入发送到转换函数:
However, I get the following error: Iterable over raw text documents expected, string object received
但是,我收到以下错误: Iterable over raw text documents expected, string object received
How do I fix this error? I also referred to thisanswer where the person says that s
is an iterable because it was assigned a string. I also came across this linkwhere a TypeError: 'String' object is not iterable
is encountered. Am I missing something here? The links seem to contradict each other.
我该如何解决这个错误?我还提到了这个答案,其中有人说这s
是一个可迭代的,因为它被分配了一个字符串。我还遇到了遇到a 的链接TypeError: 'String' object is not iterable
。我在这里错过了什么吗?这些链接似乎相互矛盾。
EDIT:I just realized that input.plot_movie
is of type unicode and decided to convert it to a string. I encounter the same error again.
编辑:我刚刚意识到这input.plot_movie
是 unicode 类型并决定将其转换为字符串。我再次遇到同样的错误。
回答by thegreatcoder
The solution to this problem is because input is just a String, but what is needed is a list (or an iterable) containing a single element (which is nothing but the String itself).
这个问题的解决方案是因为输入只是一个字符串,但需要的是一个包含单个元素(它只是字符串本身)的列表(或一个可迭代对象)。
The error can be removed by adding the following line:
可以通过添加以下行来消除错误:
input=[input]
before
前
X_new_counts = count_vect.transform(input.plot_movie)