Guest User

Untitled

a guest
Feb 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. from sklearn import datasets
  2. import pandas as pd
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. from numpy.linalg import inv
  6.  
  7. iris = datasets.load_iris()
  8. X = iris.data[:100, :]
  9. y = iris.target[:100].reshape((100, -1))
  10.  
  11.  
  12. def logit(x):
  13. return 1. / (1 + np.exp(-x))
  14.  
  15.  
  16. m, n = X.shape
  17. alpha = 0.0065
  18. theta_g = np.random.random((n, 1))
  19. maxCycles = 30
  20. J = pd.Series(np.arange(maxCycles, dtype=float))
  21.  
  22. for i in range(maxCycles):
  23. h = logit(np.dot(X, theta_g))
  24. J[i] = -(1 / 100.) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
  25. error = h - y
  26. grad = np.dot(X.T, error)
  27. theta_g -= alpha * grad
  28. print theta_g
  29. J.plot()
  30. plt.show()
  31.  
  32. theta_n = np.random.random((n, 1))
  33. maxCycles = 1
  34. C = pd.Series(np.arange(maxCycles, dtype=float))
  35. for i in range(maxCycles):
  36. h = logit(np.dot(X, theta_n))
  37. C[i] = -(1 / 100.) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
  38. error = h - y
  39. grad = np.dot(X.T, error)
  40. A = h * (1 - h) * np.eye(len(X))
  41. H = np.mat(X.T) * A * np.mat(X)
  42. theta_n -= inv(H) * grad
  43. print theta_n
  44. C.plot()
  45. plt.show()
Add Comment
Please, Sign In to add comment