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’