Week 2 practice lab help - Exercise 1

Hi everyone

I have a little problem with solving the week 2 practice lab. (Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera). The way I tried to implement Q1 was to initialize the b and w arrays. Then calculate y_prediction using: y_pred = np.dot(w, x_train) to make sure I got them all at once. Then I summed the cost using np.sum like this: cost = np.sum((y_pred - y_train))**2.

At the end, I implemented the * 1/2*m.

The result I got was way off. Additionally, I get this error: ValueError: shapes (4,) and (97,) not aligned: 4 (dim 0) != 97 (dim 0).

I understand that it is also possible using the way the hint provides and parsing every data point in a for loop, but I discovered this way in a YouTube tutorial and wanted to implement it like this.

Unrelated but is this too much code to post?

Thanks in advance,
Flim

If you’re discussing the compute_cost() function, then you should not use x_train inside the function.

Instead use the function parameter ‘x’.

Also, when you divide by 2*m, be sure you use parenthesis. Otherwise your code will divide by 2 and then multiply by m.

Why is that? Is regular ‘‘x’’ not all the data? So when you are training the model and computing the cost you train it on all the data?

Inside the cost function, ‘x’ represents whatever data you want to compute the cost on.

That decision is made by the code that calls the function - not within the function.

Inside the function, ‘x’ is a local variable. That’s what you should use inside the function.

x_train is a global variable. Do not use global variables inside local functions.

Hello @Flimdejong,

It is better to pay attention to the error message

ValueError: shapes (4,) and (97,) not aligned: 4 (dim 0) != 97 (dim 0).

What are you going to do with it? My suggestion is to paste and search the whole thing in Google, study a few examples Google gives you. When studying the examples, it is common to try to reproduce the errors in the examples and then solve them. If you can reproduce and solve them, you are more on top of it. After that, you could revisit the error you shared and see if you can solve it.

This is how we can learn :wink:

Cheers,
Raymond