Guest User

Untitled

a guest
Oct 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. def read_csv(filename):
  2. f = open(filename)
  3. lines = f.readlines()
  4. x = list() # Input list
  5. y = list() # Output list
  6. for line in lines:
  7. temp = line.strip().split(",")
  8. x.append([float(temp[0].replace('"', ''))])
  9. y.append([float(temp[1].replace('"', ''))])
  10. return np.array(x), np.array(y)
  11.  
  12. def regression():
  13. iterations = 1000
  14. degree = 3
  15. lr = 0.1
  16. train_test = 0.8
  17. weights = np.zeros((degree + 1, 1))
  18. inp, out = read_csv(filename)
  19.  
  20. s = np.arange(inp.shape[0])
  21. np.random.shuffle(s)
  22.  
  23. inp = inp[s]
  24. out = out[s]
  25. train_inp = inp[:int(train_test*inp.shape[0])]
  26. train_out = out[:int(train_test*out.shape[0])]
  27. hx = np.array([[train_inp[i][0] ** j for j in range(degree + 1)] for i in range(train_inp.shape[0])])
  28. for iteration in range(iterations):
  29. y = np.dot(hx, weights)
  30. err = train_out - y
  31. temp = np.dot(np.transpose(hx), err)
  32. weights = weights - 2 * lr * temp
  33. print weights
  34. print "__-__-__"
Add Comment
Please, Sign In to add comment