C1_W3_Logistic_Regression - Ex2 (total_cost np array > error)

On the last line of my code, I had to use: total_cost = (1 / m) * float(loss_sum) instead of: total_cost = (1 / m) * loss_sum as in the code hint.
Otherwise the verification code fails at: print('Cost at initial w and b (zeros): {:.3f}'.format(cost)) because cost is a np array instead of a scalar.

No if you recalled your loss sum with the right function then your total_Cost is as per the code hint

Add the bias term to z_wb

   loss =  # Your code here to calculate loss for a training example

   loss_sum += loss # equivalent to loss_sum = loss_sum + loss

See the image below

If I use the exact same code as in the hint:

        loss = -y[i]*np.log(f_wb)-(1-y[i])*np.log(1-f_wb)
        loss_sum += loss

    total_cost = (1 / m) * loss_sum

I am getting an error at cell 132:

Unless I missed something, my code is the same as in the code hint, except that I converted loss_sum which is a numpy array to a float on the last line to avoid getting the TypeError: unsupported format string passed to numpy.ndarray.__format__.

You need to make sure your f_wb has the correct code which again depends on z_wb code.
Have you intialized loss_sum to 0?? at the starting of your code as per the hint
Please send this grader cell via DM.
Do not post any code here. It is against community guidelines

I found a typo in my GRADED FUNCTION: sigmoid (UNQ_C1, cell 136). There was an extra “)”. The code executed correctly but apparently generated the TypeError the 2nd excercise. Thank you for pointing me in the right direction :slight_smile:

Cost function for logistic regression

UNQ_C2

GRADED FUNCTION: compute_cost

def compute_cost(X, y, w, b, *argv):
“”"
Computes the cost over all examples
Args:
X : (ndarray Shape (m,n)) data, m examples by n features
y : (ndarray Shape (m,)) target value
w : (ndarray Shape (n,)) values of parameters of the model
b : (scalar) value of bias parameter of the model
*argv : unused, for compatibility with regularized version below
Returns:
total_cost : (scalar) cost
“”"
m, n = X.shape[0]

### START CODE HERE ###
 Code deleted by Mentor
### END CODE HERE ### 

return total_cost

File “”, line 19
cost = 0.0
^
IndentationError: unexpected indent

Please help me for resoling issue

Please guide me where i m doing wrong

UNQ_C2

GRADED FUNCTION: compute_cost

def compute_cost(X, y, w, b, *argv):
“”"
Computes the cost over all examples
Args:
X : (ndarray Shape (m,n)) data, m examples by n features
y : (ndarray Shape (m,)) target value
w : (ndarray Shape (n,)) values of parameters of the model
b : (scalar) value of bias parameter of the model
*argv : unused, for compatibility with regularized version below
Returns:
total_cost : (scalar) cost
“”"
m,n = X.shape[0]

### START CODE HERE ###
  Code deleted by Mentor
### END CODE HERE ###

What is z_i and f_wb_i?? Please don’t make your own codes.

Follow the hints given just below the grader cell.

indentation error means you have . , ’ " somewhere in your code which is causing this error. Refer hints below the grader cell. that will help you solve the error.

Do not add any extra code lines.
Follow the template code.

Kindly remove any post comments that includes your grader cell code. It is against community guidelines. Posting again and again can effect your profile to be flagged.

@Jamal022 Can you please remove codes from this post.

Hey @Anuj_Sharma7,

As @Deepti_Prasad said make sure to don’t include any code as it’s against our community guidelines. So i deleted the code you just posted.
You can post a screenshot of the error message or screenshot of the notebook without the code used for grading.

Regards,
Jamal

Hello Anuj,

You are not suppose to change anything in the grader cell especially which is outside ##START CODE HERE AND ###END CODE HERE###

From the image you have shared where
m,n = X.shape[0]

In the fresh notebook, the above line is m,n = X.shape

Do not add anything which is already defined by the grader especially something outside where you are not suppose to write the code.

I am repeating again you are only suppose to replace None and maintain the template code. adding any new line or changing any other codes will cause you error.

I am sharing a link, please refer to it. Please read each line with patience and then do the corrections.

Let me know when your issue is cleared.

Regards
DP