Getting "AttributeError: 'int' object has no attribute 'shape'" in predict

Hello community! I hope you are doing well.

I am stuck in a “predict” function.
I followed the steps explained in “Hints”

For example, hint said:
“# Calculate f_wb (exactly how you did it in the compute_cost function above)
# using a couple of lines of code
f_wb =”
I just copied my code from the compute_cost function (except the “cost” part) and pasted it into a predict code panel. It is a two-line code, defining z_i and f_wb_i.

Furthermore, then I just define that p[i] is equal to f_wb_i and then use the if-else statement for p=1 or 0.

But I am getting this error:


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’


If you need to look at my notebook, I will inbox you. Kindly guide me, I will be highly indebted to you.

Regards,
Saif.

You don’t have to compute the cost function at this stage (but just make use of some of your steps in that calculation).
I believe you would already have the w and b from the regression. Use them and the X to predict (i.e., to model) y.
If you have calculated the cost function, and because it is a scalar, that’s probably why you’re getting this error.

And for p, think about the boundary value of 0.5.

Yes, I did not compute cost but used some lines of code to compute f_wb_i. Regarding p, I set it to >= 0.5. Can I inbox you my notebook?

You could, and I could try to see if i can help and spot the error(s).

Thanks for sending the notebook.

There are several things I spotted.

  • indentation matters in python. After you calculated z_i and f_wb_i within the loop, p[i] should also be in the loop, because you are calculating the i-th element in the sample. This also applies to your if-else case. All those lines should be within the for loop.

  • Then within the if-else case, p[i] instead of p should be used

Hope this helps!

(Alternatively, instead of having if-else within the loop, you can apply a mask to edit p after you updated all elements in p. By this I mean something in this format:

p[your criteria on p] = 1
1 Like

Hello! Thanks for your guidance. I followed it and got everything right, 100% score. I am wondering to become like you. I am new to Python, any suggestions/courses? I am highly indebted to your help.

In the last, while I got 100% in this assignment, I got this error (attached file). Moreover, my model (with regularization) “Train Accuracy: 82.203390” but Expected Output:
Train Accuracy:~ 80%

If you explain this to me in your leisure time, I will be truly thankful to you.

For the ~80% part, I think the 82.203390 is correct (I got the exact same number in my submission). After all, ~80% means about 80%, and the answer you got fits that description.

As for your screenshot, I believe you accidentally use X_train instead of the original X_mapped (this is at the very end of the lab, right?).
With X_train, it is in the first half of the lab, and X_train only has 2 features (hence, 2 columns). But for X_mapped, it refers to the 2nd half of the lab; X has been mapped to high-order polynomials and thereby become a 27-dimensional vector (See the section of feature mapping in this lab). Hence, the new w that you obtain has 27 terms and does not match the dimension of X_train. This yields the error message.

Finally, for python learning, everyone will get better after using it more frequently :slight_smile:
(I am new to machine learning but have been using python for many years.)
I don’t have particular suggested resources for learning python. But if you search for “python tutorial”, there are many free resources that will guide you through basics (e.g., conditional statements, loops, functions, etc.).
Or perhaps the mentors here may have good python learning suggestions?

2 Likes

@saifkhanengr, please check out this post for some suggestions about learning Python.

Cheers,
Raymond

1 Like

I am truly thankful to both of you. See you in course 2.

1 Like