Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear Regression\Gradient Descent python implementation

I'm trying to implement linear regression using the gradient descent method from scratch for learning purposes. One part of my code is really bugging me. For some reason the variable x is being altered after I run a line of code and I'm not sure why.

The variables are as follow. x and y are numpy arrays and I've given them random numbers for this example.

x = np.array([1, 2, 3, 4, ...., n])
y = np.array([1, 2, 3, , ...., n])
theta = [0, 0]
alpha = .01
m = len(x)

The code is:

theta[0] = theta[0] - alpha*1/m*sum([((theta[0]+theta[1]*x) - y)**2 for (x,y) in zip(x,y)])

Once I run the above code x is no longer a list. It becomes only the variable n or the last element in the list.

like image 849
user1596466 Avatar asked May 09 '26 02:05

user1596466


2 Answers

What is happening is that python is computing the list zip(x,y), then each iteration of your for loop is overwriting (x,y) with the corresponding element of zip(x,y). When your for loop terminates (x,y) contains zip(x,y)[-1].

Try

theta[0] = theta[0] - alpha*1/m*sum([((theta[0]+theta[1]*xi) - yi)**2 for (xi,yi) in zip(x,y)])
like image 60
C. M. Avatar answered May 10 '26 16:05

C. M.


Yes, x is being reassigned in your list comprehension. Why not just change the variable name used there so that it won't get overwritten?

theta[0] = theta[0] - alpha*1/m*sum([((theta[0]+theta[1]*x_i) - y_i)**2 for x_i, y_i in zip(x,y)])
like image 42
monkut Avatar answered May 10 '26 14:05

monkut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!