Sms_text_classicator Proyect from Machine Learning

I have already done the 5* project of Machine learning where you have to create a model to predict if the messages are spam or are ham. I have created, and fit the Dense model. And it is working properly but now I have to create the function that passing the sms as a parameter , predict if it is spam or ham.
I will need your help because I am stuck without moving and I do not know how to solve the error.
Basically, when I create the method it is called passing just one message, and it is working, but the problem appears when I try to run the last cell with the test, because in that occasion, The message passed as a parameter are a tuple, and it is when the error shows.
These are my 2 last cells:

function to predict messages based on model

(should return list containing prediction and label, ex. [0.008318834938108921, ‘ham’])

def predict_message(pred_text):

new_sequences= tokens.texts_to_sequences(pred_text)
padded =pad_sequences(new_sequences,maxlen=50, padding=“post”, truncating=“post”)
pred= np.array(model.predict(padded))

if pred<=0.5:
var=“ham”
else:
var=“spam”

return (pred, var)

pred_text=[“how are you doing today?”]
prediction=predict_message(pred_text)
print(prediction)

When I run the cell, this is the output. But, later on, I have the issue in the next cell.
(array([[0.00122303]], dtype=float32), ‘ham’)

In this last cell, that I suposse to do not change is when I find the error:

Run this cell to test your function and model. Do not modify contents.

def test_predictions():
test_messages = [“how are you doing today”,
“sale today! to stop texts call 98912460324”,
“i dont want to go. can we try it a different day? available sat”,
“our new mobile video service is live. just install on your phone to start watching.”,
“you have won £1000 cash! call to claim your prize.”,
“i’ll bring it tomorrow. don’t forget the milk.”,
“wow, is your arm alright. that happened to me one time to” ]

test_answers = [“ham”, “spam”, “ham”, “spam”, “spam”, “ham”, “ham”]
passed = True

for msg, ans in zip(test_messages, test_answers):
prediction = predict_message(msg)
if prediction[1] != ans:
passed = False

if passed:
print(“You passed the challenge. Great job!”)
else:
print(“You haven’t passed yet. Keep trying.”)

test_predictions()

And I do not know how to solve this error without changing this last cell:


ValueError Traceback (most recent call last)
in ()
23 print(“You haven’t passed yet. Keep trying.”)
24
—> 25 test_predictions()

1 frames
in predict_message(pred_text)
6
7 pred= np.array(model.predict(padded))
----> 8 if pred>=0.5:
9 var=“ham”
10 else:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Post a link to your jupyter notebook on colab and I’ll take a look. It should be as easy as iterating over the messages and feeding them to the model, but there are some little details to get right moving back and forth from python lists to numpy arrays.

Than you Jeremy. This morning I have just completed the challenge. Last night I were thinking and finally I created a separate function to create the correct iteration with a lambda and then I called this one from the test_predictions() to give out the perfect array.
Then I passed the last cell without any issue or changes.
Thank you for your help. This it is already solved.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.