Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 "__-__-__"
Add Comment
Please, Sign In to add comment