Assignment: test_logistic_regression "unsupported format"

Good day,
Despite trying many ways and even I’ve been unable to get any results in my test_logistic_regression function, as I get the same error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-8efee5aca39c> in <module>
      1 tmp_accuracy = test_logistic_regression(test_x, test_y, freqs, theta)
----> 2 print(f"Logistic regression model's accuracy = {tmp_accuracy:.4f}")

TypeError: unsupported format string passed to numpy.ndarray.__format__

I am aware I need to change dimensions so that the function works, but despite using np.array(), I keep getting the same error.

Any ideias?

Hi @ibsen.gr

Just print the tmp_accuracy variable and see what it looks like (in a new cell or without :.4f). I’m guessing your tmp_accuracy is an array and not a scalar (a float representing accuracy).
Note that “accuracy” is a number ([0.0 … 1.0]) representing how many tweets you got right out of all the tweets.

Cheers

Hi @arvyzukai ,
Thank you for your answer-

It seems like I am not shaping correctly, because as I was trying to correct dimension, I got the following error


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-8efee5aca39c> in <module>
----> 1 tmp_accuracy = test_logistic_regression(test_x, test_y, freqs, theta)
      2 print(f"Logistic regression model's accuracy = {tmp_accuracy:.4f}")

<ipython-input-55-4e2d30be3c10> in test_logistic_regression(test_x, test_y, freqs, theta, predict_tweet)
     20         y_pred = extract_features(tweet, freqs, process_tweet=process_tweet)
     21 
---> 22         if y_pred > 0.5:
     23             # append 1.0 to the list
     24             y_hat.append(1.0)

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

Ask Chat GPT for a review of the code that you have just submitted

The message is pointing you to the error: in that expression y_pred is a numpy array, so if you write a Boolean expression like y_pred > 0.5, then that is also a numpy array of Booleans. So it doesn’t make sense to use that as the conditional on an"if" statement.

So the question is why that happened. I think the mistake is that you should not have called extract_features to compute y_pred in the first place. Please take another look at the instructions. What does extract_features do and what is it that you need to do at that point in the code? They tell you in the instructions which function to call.

As Paul correctly noted, you are now using the wrong function. Please take a look at Exercise 5 instructions, in particular:

Use your ‘predict_tweet’ function to make predictions on each tweet in the test set.

Regarding the error you had prior:

A prediction is accurate when the y_hat equals the test_y. Sum up all the instances when they are equal and divide by m.

This one is a bit harder since you are dealing with vectorized calculations, so use the “Hints” section.

Cheers

Hi, everyone,

As you mentioned, I accidentally changed the prediction function with the wrong one (long story short, Jupyter Lab didn’t save my changes, had to redo most of my work.)

At the moment, as @arvyzukai suggested, I ran the code without .4f.

After finding out a way to turn into an 1D array, I had to sum values and it worked.

Thanks everyone!!