Help wiith Linear Regression

In this function, we want to go through all of the x values and all of the y values and compute x*(y - (m*x+b)) for each of them.

Create a variable called diff that has the sum of all of these values, and return it from the function.

#This is the code I wrote. Can someone explain why it is not correct?
def get_gradient_at_m(x, y, m, b):
diff = 0
N = len(x)
for i in range(N):
y_val = y[i]
x_val = x[i]
diff += (y_val - ((m * x_val) + b))
m_gradient = -2/N * diff
return m_gradient

What makes you say that it’s not correct?
Do you get the same result manually for the same input?
If not, then, what does it do differently, if you print out what it’s doing you can compare to what you meant.

Thanks for the reply I’ll try different inputs as suggested by you. I think the difference is my 1st function tion to go thru all y_val is correct but when I copy that to go thru all x_val I have to change formula some how?

#The mistake was in the syntax to compute the x_val
diff += x_val (y_val - ((mx_val) + b))

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.