Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import numpy as np
  2. import numpy.linalg as linalg
  3. import pandas as pd
  4. import csv as csv
  5.  
  6. # Read in data to a pandas dataFrame. This file has 3 columns, 'x1', 'x2' and 'y'
  7. data = pd.read_csv(r'.logistic_xy.csv')
  8.  
  9. # Convert to a numpy array
  10. data = np.array(data)
  11.  
  12. # Define data matricies
  13.  
  14. X = np.array(data[0::,0:3])
  15. y = np.array(data[0::,3])
  16. y = y.reshape((99,1))
  17. W = np.zeros([99,99],dtype=float)
  18. p = np.zeros([99,1],dtype=float)
  19. z = np.zeros([99,1], dtype=float)
  20.  
  21. # Define hypothesis function
  22. def hTheta(x):
  23. g = np.exp(x)/(1+np.exp(x))
  24. return g
  25.  
  26. # Define convergence variable and initialize theta
  27.  
  28. theta = np.zeros([3,1],dtype=float)
  29. thetaNew = theta
  30. conv = 1
  31.  
  32. while conv > 0.01:
  33. for i in xrange(0,int(data[0::,0].sum())):
  34. theta = thetaNew
  35. x = np.dot(np.transpose(theta),data[i,0:3])[0]
  36. h = hTheta(x)
  37. W[i,i] = h*(1-h)
  38. p[i] = h
  39.  
  40. z = np.add(np.dot(X,theta),np.dot(np.linalg.inv(W),np.subtract(y,p)))
  41. A = np.linalg.inv(np.dot(X.transpose(),np.dot(W,X)))
  42. B = np.dot(X.transpose(),np.dot(W,z))
  43.  
  44. thetaNew = np.dot(A,B)
  45. conv = np.linalg.norm(np.subtract(thetaNew,theta))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement