Skip to content
Advertisement

Iterable over raw text documents expected, string object received

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:

enter image description here

However, I get the following error: Iterable over raw text documents expected, string object received

How do I fix this error? I also referred to this answer where the person says that s is an iterable because it was assigned a string. I also came across this link where a TypeError: 'String' object is not iterable is encountered. Am I missing something here? The links seem to contradict each other.

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.

Advertisement

Answer

The cause of this problem is that input is just a string, but what is needed is a list (or an iterable) containing a single element.

The error can be removed by adding the following line:

input=[input]

before

X_new_counts = count_vect.transform(input.plot_movie)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement