Mystery Friend Chatbot Project
Hi everyone!
I am having issues with this project. It is a program that is supposed to use the bag of words model to determine which friend out of a friend group wrote a mystery letter by training the language model with previous letters written by each friend. I don’t seem to have any syntax errors and I think that I did everything correctly but I keep getting this error when I run the code.
Traceback (most recent call last):
File “”, line 47, in
mystery_friend = predictions[0] if predictions[0] else “someone else”
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The code in question can be seen below. line 47 of the code was not written by me, it is was already written in the code editor and I do not even understand how that line works.
from goldman_emma_raw import goldman_docs
from henson_matthew_raw import henson_docs
from wu_tingfang_raw import wu_docs

import sklearn modules here:

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

Setting up the combined list of friends’ writing samples

friends_docs = goldman_docs + henson_docs + wu_docs

Setting up labels for your three friends

friends_labels = [1] * 154 + [2] * 141 + [3] * 166

Print out a document from each friend:

#print(“Goldman”, goldman_docs[121])
#print(“Henson”, henson_docs[121])
#print(“Wu”, wu_docs[30])

mystery_postcard = “”"
My friend,
From the 10th of July to the 13th, a fierce storm raged, clouds of
freeing spray broke over the ship, incasing her in a coat of icy mail,
and the tempest forced all of the ice out of the lower end of the
channel and beyond as far as the eye could see, but the Roosevelt
still remained surrounded by ice.
Hope to see you soon.

Create bow_vectorizer:

bow_vectorizer = CountVectorizer()

Define friends_vectors:

friends_vectors = bow_vectorizer.fit_transform(friends_docs)

Define mystery_vector:

mys_post = list(mystery_postcard)
mystery_vector = bow_vectorizer.transform(mys_post)

Define friends_classifier:

friends_classifier = MultinomialNB()

Train the classifier:, friends_labels)

Change predictions:

predictions = [friends_classifier.predict(mystery_vector)]

mystery_friend = predictions[0] if predictions[0] else “someone else”

Uncomment the print statement:

print(“The postcard was from {}!”.format(mystery_friend))

You can use the link above to get a more in depth understanding of the question.

Please have a look at the following as it’s much easier to read formatted code- How do I format code in my posts?

That particular error occurs when you’ve attempted to use a numpy array (with more than one element) as a boolean. For example the following would throw the same error (an array with two elements-
if np.array((1, 2)): print("test")

If that code was pre-written then it suggest that there may a nested structure when one was not expected. Make sure you’ve not added an unexpected degree of nesting and if not you’ll have to start checking your working.


Thank you! At first I did not understand what the error code meant but thanks to your explanation, I was able to resolve the issue. It was actually something very fascinating.

The problem arose because in this line:

# Define mystery_vector: 
mys_post = list(mystery_postcard)
mystery_vector = bow_vectorizer.transform(mys_post)type or paste code here

The output of mys_post = list(mystery_postcard) was a list where each letter and character in every word of the entire postcard was an element.

But when I put the variable in list brackets like this ‘[mystery_postcard]’ in mystery_vector = bow_vectorizer.transform([mystery_postcard]) , then it returned a list with the entire contents of the document as one element. So you were correct, thanks for you help.

1 Like