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.