Predict function MLS Course 1 Week3

I need help on the prediction function code in the if clause.

def predict(X, w, b):
“”"
Predict whether the label is 0 or 1 using learned logistic
regression parameters w

Args:
X : (ndarray Shape (m, n))
w : (array_like Shape (n,))      Parameters of the model
b : (scalar, float)              Parameter of the model

Returns:
p: (ndarray (m,1))
    The predictions for X using a threshold at 0.5
"""
# number of training examples
m, n = X.shape   
p = np.zeros(m)

### START CODE HERE ### 
# Loop over each example
for i in range(m):   

    z_wb = 0
             # Loop over each feature
    for j in range(n): 
        z_wb_ij = X[i, j] * w[j]
        z_wb += z_wb_ij
            
    z_wb += b
    f_wb = sigmoid(z_wb)
    p = f_wb
    
    if p >= 0.5:
        p = 1
    else:
        p = 0
    
return p

np.random.seed(1)
tmp_w = np.random.randn(2)
tmp_b = 0.3
tmp_X = np.random.randn(4, 2) - 0.5

tmp_p = predict(tmp_X, tmp_w, tmp_b)
print(f’Output of predict: shape {tmp_p.shape}, value {tmp_p}')

UNIT TESTS

predict_test(predict)

AttributeError Traceback (most recent call last)
in
6
7 tmp_p = predict(tmp_X, tmp_w, tmp_b)
----> 8 print(f’Output of predict: shape {tmp_p.shape}, value {tmp_p}')
9
10 # UNIT TESTS

AttributeError: ‘int’ object has no attribute ‘shape’

As you see, p is initialized as p = np.zeros(m). It is a vector with size=m.
But, in the for-loop, you set “integer 1 or 0” to p directly. This redefined “p” as "int. And, of course, “int” has no attribute ‘shape’.
Think about why you are using for-loop with iteration count = m. You need to set calculated value into each element in p vector like p[i].

By the way, posting your code is not recommended. Please remove it.