Advertisement
Guest User

Untitled

a guest
May 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import math
  4. import csv
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7.  
  8. ###############################################################################
  9. # Question 1 #
  10. ###############################################################################
  11. # (a)
  12.  
  13. showresults = 0
  14.  
  15. def load_data(filename):
  16. """should load the data from the given file, returning a matrix for X and a vector for y"""
  17. my_data = np.genfromtxt(filename, delimiter=',')
  18. #print(my_data)
  19. return (my_data[:,0:2]), (my_data[:,2])
  20.  
  21.  
  22. X,y = load_data('ex_data.csv')
  23. m = len(y)
  24.  
  25. # extend the data in order to add a bias term to the dot product with theta
  26. X = np.column_stack([np.ones(m), X]) #TODO ???
  27.  
  28. if showresults:
  29. print("Matrix X")
  30. print(X)
  31. print("Vector y")
  32. print(y)
  33.  
  34. ## now plot the data
  35.  
  36. pos = np.where(y == 1)
  37. neg = np.where(y == 0)
  38. plt.scatter(X[pos, 1], X[pos, 2], marker='o', c='g')
  39. plt.scatter(X[neg, 1], X[neg, 2], marker='x', c='r')
  40. plt.xlabel('Exam score 1')
  41. plt.ylabel('Exam score 2')
  42. plt.legend(['Admitted', 'Not Admitted'])
  43. plt.grid(True)
  44. if showresults:
  45. plt.show()
  46.  
  47. #TODO minmaxtransform?
  48.  
  49. # (b)
  50.  
  51. def Hypothesis(theta, x):#TODO vectorize hypothesis? Test the function
  52. z=0
  53. for i in range(len(theta)):
  54. z+= x[i]*theta[i]
  55. return Sigmoid(z)
  56.  
  57. def Sigmoid(x):
  58. result = float(1.0 / float((1.0 + math.exp(-1.0*x))))
  59. return result
  60. Sigmoid = np.vectorize(Sigmoid)
  61.  
  62. if showresults-1:
  63. thetatest = np.zeros(3)
  64. print("Hypothesis theta, X[0]:")
  65. print(Hypothesis(thetatest, X[0]))
  66. print("Hypothesis theta, X[13]:")
  67. print(Hypothesis(thetatest, X[13]))
  68.  
  69. if showresults:
  70. print("sigmoid X:")
  71. print(Sigmoid(X))
  72. print("sigmoid 1:")
  73. print(Sigmoid(1))
  74. print("sigmoid 0:")
  75. print(Sigmoid(0))
  76. print("sigmoid -1:")
  77. print(Sigmoid(-1))
  78.  
  79.  
  80. # (c)
  81. def cost(X, y, theta):
  82. m = len(y)
  83. sumOfErrors = 0
  84. for i in range(m):
  85. xi= X[i]
  86. hi=Hypothesis(theta, xi)
  87. # TODO fix
  88. if hi==1:
  89. hi=0.9999999999999999
  90. error_i = -(y[i]*math.log(hi)) - ((1-y[i])*math.log(1-hi))
  91. sumOfErrors += error_i
  92. result = sumOfErrors / m
  93. return result
  94.  
  95. thetatest= [0,0,0]
  96. debug = cost(X, y, thetatest)
  97. print("Cost:")
  98. print(debug)
  99.  
  100. def grad(X, y, theta, j):
  101. m=len(y)
  102. sum = 0
  103. for i in range(m):
  104. xi=X[i]
  105. hi=Hypothesis(theta, xi)
  106. term=hi*-y[i]
  107. term= term*xi[j]
  108. sum+=term
  109. result = sum/ m
  110. return result
  111.  
  112. if showresults:
  113. debug = [grad(X, y, theta, 0), grad(X, y, theta, 1), grad(X, y, theta, 2)]
  114. print("Gradient cost:")
  115. print(debug)
  116.  
  117. # (d)
  118.  
  119. #test
  120. def GD(costf, gradf, theta0, lr, steps):
  121. theta = theta0
  122. for i in range(steps):
  123. newtheta = []
  124. for j in range(len(theta)):
  125. newtheta_j = theta[j] - (lr * grad(X, y, theta, j))
  126. newtheta.append(newtheta_j)
  127. theta=newtheta
  128. if i % (steps/10) == 0:
  129. print("************************")
  130. print("Iteraction: ", i)
  131. print("Theta")
  132. print(theta)
  133. print("cost(X, y, theta)")
  134. print(cost(X, y, theta))
  135. print("grad(X, y, theta, j)")
  136. print([grad(X, y, theta, 0),grad(X, y, theta, 1),grad(X, y, theta, 2)])
  137. return theta
  138.  
  139. """
  140. Args:
  141. costf: cost function (only needed for debugging/outputting intermediate results)
  142. gradf: gradient of the cost function
  143. theta0: initail value for the parameters theta
  144. lr: learing rate
  145. steps: total number of iterations to perform
  146. returns the final value for theta
  147. """
  148.  
  149. # (e)
  150. train = GD(cost, grad, np.zeros(3), 0.001, 100)
  151. print(train)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement