https://www.codecademy.com/paths/build-chatbots-with-python/tracks/retrieval-based-chatbots/modules/language-and-topic-modeling-chatbots/projects/bag-of-words-mystery-friend
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 “script.py”, 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_classifier.fit(friends_vectors, 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.