C1 W1 Logical error when calculating accuracy

In excersice 5, when I caluclate the accuracy, it gives me the following output:
Logistic regression model’s accuracy = 1.0000

And when testing, the follwing message appears:
Wrong output type.
Expected: <class ‘numpy.float64’>.
Got: <class ‘float’>.
Wrong output type.
Expected: <class ‘numpy.float64’>.
Got: <class ‘float’>.
2 Tests passed
2 Tests failed

Is this due to my calculation or an issue with the 1D arrays? When I print the output, all array elements are 1s.

Hi @Lujain_Andijani

The unit test basically tells you that with it’s own input it expected to get a NumPy array, but with your current implementation, it got float type value.

The mistake is probably is in accuracy calculation line, where the code hint suggests:

    # With the above implementation, y_hat is a list, but test_y is (m,1) array
    # convert both to one-dimensional arrays in order to compare them using the '==' operator

In other words,

  • y_hat is a list with prediction (type list), which you need to convert to numpy array
  • then you need to compare the y_hat with y_test (note the shapes - according to docstring y_test is (m, 1) vector, while depending on your implementations y_hat is probably (m, ) vector)
  • after comparing you need to count the correct predictions (you can just sum the compared array where True values will be treated as 1’s, so by summing you get the number your model predicted correctly)
  • and at last, you need to divide this sum with number of elements (to get the proportion your model got correctly)

Note, that these steps is a guide how to get accuracy, but all of them can be implemented with one line.

Cheers

1 Like

I have converted y_hat to be (m, 1) vector following every step in the guide, but I still face the same issues I mentioned.

To be clear, I made a one line for loop inside the sum with an if condition, I know no solution other than that.

That is not how you’re asked to compare those arrays:
# convert both to one-dimensional arrays in order to compare them using the '==' operator

In other words, do not use the for loop.

1 Like

Then, how can I access all elements for compareson?

With numpy you can compare arrays of the same size simply by using == operator or np.equal (docs).

a = np.array([2, 4, 6])
b = np.array([2, 4, 2])

a == b
# output:
array([ True,  True, False])

Well I discovered the reason for the error, I used an ‘if’ statement which case this error, thank you so much for the patiens!