C1_W1_Optional Lab Cost Function: cost_sum = 0?

Greetings! I have a question about the codes in the function compute_cost . Specifically, why cost_sum is equal to 0 first, then cost_sum = cost + cost_sum later in the loop ?

Is there a mathematical reason for that, or is it for the loop’s sake? And which part of the lecture covers or relates to this code?

I look forward to hearing your answers.

Hi there :slight_smile:

So in that cell, if you look at the two cells above, you can see the math behind calculating the cost function.
J(w,b) = \frac{1}{2m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)})^2 \tag{1}

As you can see, the total cost is a sum. Now all that the loop does is calculate that sum.

  # number of training examples
  m = x.shape[0]     # take the number of examples in the training data.
    
    cost_sum = 0     # initialize the sum to zero (to store the result of the sum)
    for i in range(m): 
        f_wb = w * x[i] + b     # this is the prediction of our model for each x, given w and b
        cost = (f_wb - y[i]) ** 2     # this is the squared error for that sample. (remember that our cost is the Mean Squared Error i.e. MSE)
        cost_sum = cost_sum + cost      # add the cost of this sample, to the sum
    total_cost = (1 / (2 * m)) * cost_sum     # now we have the sum. we devide by (2 * m) as shown in the equation.

    return total_cost

Here I tried to comment the code to be more readable. Tell me if your question remains.

1 Like

The goal is to compute the total cost by adding together the portion of the cost that is calculated for each example. That’s what “cost_sum = cost_sum + cost” does.

First the total cost has to be set to 0, so that this line of code has something to reference on the fist pass through the for-loop.