Error_linear regression_ week 2

Hello there!
I am going to write a function for model in linear regression _week2 (optional lab: C1_W2_Lab02_Multiple_Variable_Soln). My goal is, creating a vector for
prediction (f_wb = w.x +b), each element of this vector will be a prediction for corresponding example. however unfortunately I got the following error. Please help me to remove this error. Thank you all in advanc!


Hello @Mahsid_Khazaei_Shad, you have defined p as integer, yet you are trying to treat as an array by indexing it.
I think you should get familiar with python language in order to getting ahead in this course.
This is a good course for you getting familiar with programming in python.

Hi Mahshid!,

This exercise aims to optimize the execution time by vectorizing the implementation. We will need a function that returns the scalar prediction (b) from the multiplication of the vectors model features (X) and model parameters (w).

Notice that X is a vector and, X[i] is a scalar. Since w is also a vector, it is not correct to perform the operation np.dot(X[i], w) + b.

Therefore the correct implementation would be p = np. dot(X, w) + b.

Cheers,

1 Like

Hi @Enzo_Faliveni_Alzuet, p = np.dot(X, w) + b is correct. But I think we can iterate here for each row in the matrix X (calulating dot product of w with every row of X using for loop).
as X is a matrix, so X[i] will be a vector corresponding to ith row, so it should not be a scalar.

2 Likes

Hi @ritik5! I’m sorry, I didn’t see your answer, and yes, you are correct. I think I corrected the error by editing the previous comment!

Thanks a lot!

1 Like

Hello ritik 5. thanks for the response. you are right. Since I am beginner in Python then this is a good way to be familiar with.

1 Like

Hello Enzo. thank you for the reply. However in this optional lab, X (caps lock) is defined as a matrix with (m,n). then when I wanted to use dot function I wrote X[i] to consider i_th example. then X[i] is not a scalar.

Does the error correspond to section 3.2 Single Prediction, vector?

No this is for multiple features.

No this error is for multiple features.

I have the other question in this lab (optional lab: C1_W2_Lab02_Multiple_Variable_Soln). please consider the following code. I am wondering if when we are making this loop “for i in range(m)”, m is 3, then i=0,1,2 then for each “i” , a corresponding cost should be produced I mean we should have 3 costs. but here we have only one cost in the result. I am really puzzled!!!please help me. thx

Hello!

You are absolutely right that it had computed 3 costs if the loop ran 3 times, but the 3 costs had been accumulated to 1 scalar variable by this line

    cost = cost + (f_wb_i - y[i])**2

So when i is 0, (f_wb_i - y[i])**2 computed the first cost and then immediately got added to the cost variable. The same happened for the second and the third cost. The consequence is the cost keeps increasing over the loops and finally it sums over all the costs.

This accumulation technique is often used in this specialization when implementing the mathematical summation sign that basically you will see in every cost and gradient computation, so it’s really good that you are aware of it :wink:

Cheers,
Raymond

2 Likes

I am very thankful for your great response. I have understood my fault.