I want to develop a function to calculate polynomial coefficients. I take 80% of my input for training and calculate all powers of input in hx as below:
def read_csv(filename):
f = open(filename)
lines = f.readlines()
x = list() # Input list
y = list() # Output list
for line in lines:
temp = line.strip().split(",")
x.append([float(temp[0].replace('"', ''))])
y.append([float(temp[1].replace('"', ''))])
return np.array(x), np.array(y)
def regression():
iterations = 1000
degree = 3
lr = 0.1
train_test = 0.8
weights = np.zeros((degree + 1, 1))
inp, out = read_csv(filename)
s = np.arange(inp.shape[0])
np.random.shuffle(s)
inp = inp[s]
out = out[s]
train_inp = inp[:int(train_test*inp.shape[0])]
train_out = out[:int(train_test*out.shape[0])]
hx = np.array([[train_inp[i][0] ** j for j in range(degree + 1)] for i in range(train_inp.shape[0])])
for iteration in range(iterations):
y = np.dot(hx, weights)
err = train_out - y
temp = np.dot(np.transpose(hx), err)
weights = weights - 2 * lr * temp
print weights
print "__-__-__"
I use this equation for updating weights: w(t+1) <- w (t) - η * Gradient(RSS(w(t))) where Gradient(RSS(w(t))) is: -2*H.transpose * (y-H*w) But when I run my code, weights became nan after some iterations. Here is the data file.