Hi folks,
it seems my prediction is right, but 1 out of three tests seemed to fail, see here
Do I miss something? Please let me know your thoughts and ideas
I tried an array around the array, but that does not help regarding the wrong shape error
Greetz
Frank
You are using A2[0] for the comparison, so you are indexing off the first dimension. That gives an output that is a different shape than the input. The output will be a 1D vector, instead of a 2D vector.
It’s fine to do a Boolean comparison to a full array: it just gives you an output of the same shape with the elementwise comparison.
2 Likes
Dear Paul,
you made my day ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/google/slight_smile.png?v=10)
It works if I just write
Predictions = (A2 > 0.5)
Nevertheless, I tried the following as well
return [predictions]
This also created a 2d array, but seems not accepted by the autograder.
Thanks a lot
Frank
2 Likes
Hi, Frank.
I’m glad to hear that you found the solution. If by returning [predictions], you mean doing it your original way like this:
predictions = (A2[0] > 0.5)
print(f"predictions.shape = {predictions.shape}")
return [predictions]
It turns out that doesn’t quite do the same thing as the correct solution that you showed. Here’s the result I get with the bracket solution:
parameters, t_X = predict_test_case()
predictions = predict(parameters, t_X)
print("Predictions: " + str(predictions))
print(f"predictions.shape = {predictions.shape}")
predict_test(predict)
predictions.shape = (3,)
Predictions: [array([ True, False, True])]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-32-1a72e1f3efc9> in <module>
3 predictions = predict(parameters, t_X)
4 print("Predictions: " + str(predictions))
----> 5 print(f"predictions.shape = {predictions.shape}")
6
7 predict_test(predict)
AttributeError: 'list' object has no attribute 'shape'
So you can see that the type of the result is still not correct.
You could make your 1D version work by doing this instead:
return np.reshape(predictions, (1,-1))
But we already have a simpler and cleaner solution, so what’s not to like about that?
Cheers,
Paul
1 Like