For loop Exercise 3 - week 2

Hi all,
I have trouble understanding what L is in exercise 3, CW2_W! assignment.
I would happily appreciate some help.
Thank you.

Hi @Agnese! Welcome to your community!

You mean this L?

If so, it just measures how much the values f^{i}(\omega) are far from their mean, defined as \overline{f(\omega)}. So a higher value os \mathcal{L} means that the values f^{i}(\omega) have very distinct values, whereas a low \mathcal{L} means that f^{i}(\omega) have closer values.

This is the definition of the variance.

I hope that helps.


Dear Agnese,

Let me expand a bit further here. I was not able to reply to your other thread regarding Week 1. So, let me do it here:

  1. The goal of the problem is to find the percentages of the amount of products that you will buy from company A and company B so that you will use the less amount of money.
  2. This percentages are represented with decimals like this 0.2 = 20% and they are stored in the variable omega (w). So, because you only have two companies A, B. You can tell that the percentage of products that you buy from company A is: w*pa and from company b (1-w)*pb. Remember 1 means 100% so this is why the percentage for company b is 1-w
  3. This means that the total amount of products you buy is what you buy in company A + what you buy in company B. In other words: paw+pb(1-w).
  4. Now, since you need to find the optimal value for w, then you need to evaluate how good w is everytime the algorithms make an iteration (runs). Consequently, you need a cost function, and in this case the cost function is done by calculating the outcome with the current value of w vs the actual data you have in the csv file (they are using the average). So, you start with a random w and calculate the result using the formula paw+pb(1-w) then compare subtract the average of the ones you have in the csv: f(w)-average(f(w)). Then to make it only positive numbers you square. Then sum all and divide into the amount of samples and bingo! you have the cost function which is named Loss of omega or L(w).
  5. Now that you have a cost function you are able to use it to calculate how good is w in each iteration.

I hope this helps even more!

1 Like

Good morning Yeison,

And thank you for your explanation.

What you shared is clear to me. I will try to apply it to question 3 for L and L_array.

In L_array I have to set a point and a value (as explained in the exercise notebook, although explained for a function)

Let’s see how that goes.

I will be back to you.

For now, I thank you and I wish you a good day.


(Remember I am speaking about Week1. Not sure why the title says week2)

That is correct Agnese.

In exercise # 3 all what you got to do is:

  1. You already defined the python function L_of_omega which is the one we discussed previously.
  2. Now, you will calculate the output of the function after passing the values you have in the CSV file and you will store the output in an array named L_array. To do this you will:
    a. Create an array with a range of values from 0 to 1 (remember omega goes from 0 to 100 because it represents a percentage but we are using the scale 0-1)
    b. Create an array (L_array) with zeros where you will store later the output of the function
    c. For each one of the values in the range from 0 to 1 created in step #a calculate L_of_omega and store it in the L_array (Remember L_of_omega will use f_of_omega which uses the values from the CSV file)
    c. Finally, find in L_array the smallest value. Remember that L_array is the cost function and this is why you need to optimize it by finding the smallest value. Once you find the smallest cost functions then you find the omega value that generated this cost.
    d. That’s it! Now, you completed step # 3
1 Like

Good morning Yeison,

And thank you for your message.

Tomorrow or Friday I tackle the test again.

Today I have finished week two and pass the test for optimizing 2 variables -:blush:

I will be back to you soon.


Go for it Agnese! I know you will succeed!

Good morning Yeison,
my apologies but I have not succeeded.
I understand the theory but the coding here for me it is an issue.
I am sure it is not difficult, as it has been for the other test and quizzes so far, but it seems it does not get to me.
As you can see, I do not get the supposed results.
I am not also sure about L_array and the at(point) and set(point)
In the exercise book, we have to substitute point 2 with a value of 4.
Here I have a whole range…

Thank you for your time and patience.

No worries Agnese. It is the same for me and for everybody. That is why we are here to learn!

Now, please find my comments:

  1. The first thing you need to fix is related to a for loop. So, if you have the following vector
    vector = [5,7,9,1]
    and then you want to select each one of the numbers in the vector you can do a for loop that runs 4 times (because you have 4 items in the vector).

A for loop in Python will look something like this:

for i in range(4):
do something here

as you can see there is a variable i that will hold the result of the function range(4) which are the first 4 numbers starting from 0. In other words, 0 1 2 3

So, if you want to take each one of the values from the vector, the variable i will represent the index of the data inside the vector. Something like this:

for i in range(4):

  1. Now, in our case we have an array with the values of omega like in my example my vector, and we want to take each one of the values inside of the omega_array and then pass this value to the function named L_of_omega. How, do you think we can take the values of omega_array?

It will be something like this:
for i in range(N):
L = L_of_omega(omega_arrray[i])

Let me know if you are able to follow me until this point and then we can continue.

If you are following let me know what is the first error that we have discovered so far in your code.

1 Like

Hello Yeison,
and thank you for your answer.
Yes, I have understood your example about vector [n=4], thank you.
I hope I have coded correctly for L.
It run ok but expected result is off, hopefully because now only L_array needs to be fixed.
Please be so kind to explain me how to correct what I have for L_array.

I see that I have to do the same reasoning also for question 4.
Thank you so much for your help.

Bingo! You did it right for L

Now, within the for loop you are calculating the value of L with each one of the omega values in the array. The idea is to store each one of these values in the L_array. But, currently you are saving in the position i the value of N. But, you want to store L. So, what do you think you need to change here:

L_array =[i].set(N)

So that you can store the L calculated in the previous line instead of N?


Good morning Yieson,

I have to substitute N with L.
This is what I did, and after running the function, outcomes match expected result.

I have also gone ahead and tried to answer question four, using your suggested.
I have able to match the outcomes of the functions in Q4 with expected results.

I have not answer your question in your previous email: what mistakes I have found in my coding. The mistake I was doing was to pass the range of the array (0 and 1) into , instead of every number (i) of the range itself.

I do thank you for your patience and explanation.
I have a question related to the next in week 2. I have done that and passed, but I have made a mistake and I would like to understand that, so I will write you a separate message for that.
Thank you Yeison.

Question Test week 2.

Hi Yeison,

in the quiz for week 2 (Optimization Using Gradient Descent: Linear Regression)
For exercise 4, I have to increase the dimension of X using the function np.newaxis (as used in exercise 2)
As you see from the

code I use, I get the correct result, but when the shape of the matrix is checked is not (3,1), but (3).
I am not going to update the quiz and changing the code and re-submit the quiz, so: shouId write[:1, np.newaxis]?

Thank you for your answer.

Hi Agnese,

On the right track. Just mixed up the variables. Replace ‘X_pred’ with ‘X’ in X_2D. Then plug in ‘X_2D’ for ‘X’ in Y. Although they still work, your m and b should be _sklearn, not _numpy.

Should look like this:

X_2D = X[:, np.newaxis]
Y = m_sklearn * X_2D + b_sklearn

Hope this helps.


Dear Agnese,
Please find my comments:

  1. The instructions say you need to use sklearn to predict Y.
  2. The goal of the laboratory is to teach you that you can use linear regression to find the equation of the line to predict the values. However, there are libraries (Numpy, Sklearn) that allows you to do this just by calling a function. Yet, since you are learning the math behind the library, the laboratory makes you find the line equation using Numpy, then using Sklearn and finally manually calculating it by using an mathematical algorithm named gradient descent.
  3. Finally, you will compare the line equation found by using Numpy, Sklearn and manually doing it (gradient descent) which at the end is the same result.
  4. So, in exercise number 4, the goal is for you to calculate the line equation using the Sklearn library this is why the instructions say you need to use the function lr_sklearn.predict
  5. As you can see when you are declaring the function named pred_sklearn you are defining 2 arguments for the function: X, lr_sklearn.

The fist argument is the actual data from the csv file and the second one is a variable that holds an object from the sklearn library. This way, you can use the function predict.

  1. Consequently, your issue here is that you need to pass the array X_2D (please note that X needs to be reshaped in a way that the function lr_sklearn.predict expects it) to the function lr_sklearn.predict. So, if you arlready have X_2D and now to calculate Y you need to use lr_sklearn.predict() but passing X_2D as argument. What would be the code needed to calculate Y?

Good moning Yeison,
as explained by David.

Thank you so much for your help.


Good morning David,

thank you so much.
I have redone the assignment and it worked.

Good morning Yeison,

I hope my message finds you well.
I have started working on the Assignment: Neural Network with two layer (C2_W3_Assignment).
I do not understand what I write working in my code: it says @wrong output of Z1 for X@, but the value I obtain match with the expected ones.

Thank you for your time and help.
I wish you a good day.

I have passed the assignment with 100/100, but I would like to understand the reason I get a wrong value for Z1 for X.
Thank you for your answer.

Dear Agnese,

Could you please:

  1. Edit the title and write something like Week 1 and Week 2. So, if somebody else is looking for an answer they might find this post. Ideally, there should be one subject per thread to keep things organized.
  2. Create a new post for the week C2 W3 Assigment? This way it will be easier for a person in the feature to find it and might benefit with your question and the answer given?