All tests are passing for the previous cells but when it comes to combining them all into one program, the following error pops up and I don’t know where to start to fix this issue.
IndexError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)
~/work/release/W2A2/public_tests.py in model_test(target)
123 y_test = np.array([0, 1, 0])
124
→ 125 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
126
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
30
31 # Predict test/train set examples (≈ 2 lines of code)
—> 32 Y_prediction_test = predict(w, b, X_test)
33 Y_prediction_train = predict(w, b, X_train)
34
in predict(w, b, X)
33 Y_prediction[0,i] = 1
34 else:
—> 35 Y_prediction[0,i] = 0
36 # YOUR CODE STARTS HERE
37
IndexError: index 3 is out of bounds for axis 1 with size 3
Please pay attention to the shape of Y
in predict
. It should be initialized to shape (1, number of examples)
My guess is that there must be something wrong in your predict
function that wasn’t caught by the unit tests for that function. Notice that the for loop they gave you in the template code uses the shape of the A value to determine the limits on the loop. But the second dimension of A should be the same as the second dimension of X in predict
, right? So how could it happen that would not be true. E.g. did you “hard-code” some dimension or reference a global variable instead of the actual parameters?
Try putting these print statements in your predict
function right before the
“for” loop:
print(f"X.shape = {X.shape}")
print(f"A.shape = {A.shape}")
I did that in my code (plus added some other prints) and here’s what I get when I run that test cell for model
:
type(X_train) <class 'numpy.ndarray'>
X_train.shape (4, 7)
X_test.shape (4, 3)
Y_test.shape (3,)
num_iterations 50 learning_rate 0.01
X.shape = (4, 7)
A.shape = (1, 7)
X.shape = (4, 3)
A.shape = (1, 3)
Y_prediction_train [[1. 1. 0. 1. 0. 0. 1.]]
Y_prediction_test [[1. 1. 0.]]
All tests passed!
Note that my code did the train predictions first. So there are 7 samples in the train data and 3 in the test data in this one test case. So what do you see from your code?