Challenges with week 3 test Q.4

I have been looking through the labs to see if I can find examples for Q4. Because of the two for loops:
for i in range (m):
for j in range (n):
That we are required to loop through X and w, and then cast b, rather than use dot.product.

I am getting the following error message and am stuck.

Happy to share my code to see if someone can point out the error.

The hint suggests"*

  • Calculate f_wb (exactly how you did it in the compute_cost function above)*

The z_wb term in the cost function utilised dot product and sigmoid and doesn’t require the use of the i and j loops. I am confused.

Any pointers would be appreciated.

Hi Ian, Welcome to the community!

It looks like the issue arises from how you’re handling the bias term b and the term z_wb in your predict function. Specifically, the error TypeError: 'float' object is not iterable indicates that you might be attempting to add a scalar value (your bias term b) to a NumPy array (probably z_wb) in a way that Python doesn’t recognize as valid. The way you wrote z_wb += b may have confused if z_wb is not initialized properly, or if the dimensions of b and z_wb are incompatible.

As you mentioned, there are two loops iterating over m (number of examples) and n (number of features). You need to make sure that you’re calculating z_wb as a scalar for each example i in m. You can achieve this by summing over the dot product of the weights and features and adding the bias b afterward.

The hint about calculating f_wb is suggesting you follow the same process as in the cost function:
- Calculate the linear combination of inputs and parameters z_wb = np.dot(...) + ... where X[i] corresponds to the ith example
- Calculate the sigmoid of the linear combination: f_wb = sigmoid(z_wb)

I hope this helps!

Thank you. Q4 solved. but now I am getting this error with Q. 5.

The syntax of that line looks okay. I suspect the problem in the preceding few lines. Python doesn’t always highlight the line with the actual error.

As @danieljhand mentioned, this error indicates that there is probably a small typo or syntax error somewhere in the lines above it. Here are a few things to check:

  • Make sure that cost_without_reg and reg_cost are defined correctly.

  • Make sure the parentheses in reg_cost are properly enclosed.

Python throws a syntax error at the point where it gives up trying to make sense of the code.

So the error always lies somewhere prior to the error marker.

ky you for your help