Iterable over Raw Text Documents Expected, String Object Received

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:

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.

7

2 Answers

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)
0

Input should be in Square Brackets.

input = ["input"]

cv = CountVectorizer()

cv.fit(input)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.