Gradient descent for linear regression

Hi everyone,
I am trying to implement gradient descent for linear regression.
The problem is that I could not figure out how to find the optimal model parameters.
Please have a look on the figure attached with.

I would highly appreciate your kind feedback.

That does not look like it’s part of any of the assignments here in this course. I hope that what you’re asking is not for us to do your homework for you in some other course :nerd_face:.

With any kind of programming task, one really important first step is to make sure that you understand what your input data is. What format is it in and what does it represent? What is the df parameter that is passed into your function? From the way you treat it, I’m guessing that it is a python dictionary that contains numpy arrays. If I’m correct in that guess, then the expression df.shape[0] (which you are using all over the place) is just going to throw an error, because dictionaries do not have shape as an attribute. Also notice that you appear to be looping over the samples and you do not mention the parameter num_iterations anywhere in your code. You should be able to vectorize the “per sample” part of the computation by using matrix operations and the loop should be over the number of iterations of gradient descent.

But as I said at the outset, the first thing is to understand the format and structure of the input data. Print the type and shape of the X and y variables that you are extracting from df.

Then the second step would be to make sure you are clear on the mathematical formulas that express what you are trying to do with gradient descent. The point here is translating the math into python code of course, so it is critical to start with a clear understanding of the math.

1 Like