Skip to content

Instantly share code, notes, and snippets.

@grohith327
Created May 27, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grohith327/6f3a733dc90fc3595bad43d4f5658a2a to your computer and use it in GitHub Desktop.
Save grohith327/6f3a733dc90fc3595bad43d4f5658a2a to your computer and use it in GitHub Desktop.
## Linear Regression
import numpy as np
n = 700
alpha = 0.0001
a_0 = np.zeros((n,1))
a_1 = np.zeros((n,1))
epochs = 0
while(epochs < 1000):
y = a_0 + a_1 * x_train
error = y - y_train
mean_sq_er = np.sum(error**2)
mean_sq_er = mean_sq_er/n
a_0 = a_0 - alpha * 2 * np.sum(error)/n
a_1 = a_1 - alpha * 2 * np.sum(error * x_train)/n
epochs += 1
if(epochs%10 == 0):
print(mean_sq_er)
@OregonMortensen
Copy link

I think your work is very good, but somehow i am messing it up. When I try to run the code the error goes toward infinity instead of toward zero. This leads to a runtime overflow error and all values in error and y becomes NaN.

Any ideas what could cause this?

@imnaren142
Copy link

I think your work is very good, but somehow i am messing it up. When I try to run the code the error goes toward infinity instead of toward zero. This leads to a runtime overflow error and all values in error and y becomes NaN.

Any ideas what could cause this?

Hi @OregonMortenson add these lines in respective locations
y_train = y_train.reshape(-1,1)

a_0 = a_0[0:300]
a_1= a_1[0:300]

it will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment