Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. import numpy as np
  2. import scipy.io as sio
  3. import scipy.misc as misc
  4. import matplotlib.pylab as plt
  5. import matplotlib.cm as cm
  6. from PIL import Image
  7. from imageHandler import getMatrix
  8. def sigmoid(x):
  9. return 1/(1 +np.exp(-x))
  10.  
  11. def findingTheta(x, y, num_labels, alpha, num_iterations):
  12. n = (x.shape[1])
  13. m = y.shape[0]
  14. x = np.append(np.ones((x.shape[0],1)),x,axis=1)
  15. all_theta = np.zeros((num_labels,n+1)) # all_theta = zeros(num_labels, n+1)
  16. theta = np.zeros((n+1,1))#nitial_theta = zeros(size(x, 2))
  17. for i in range(1,num_labels+1):#sprawdzic czy 0 tez jest liczba
  18. y_2 = (y==i).astype(int)
  19. for j in range(num_iterations):
  20. theta = theta-(alpha/m)*(np.dot(x.transpose(),(sigmoid(np.dot(x,theta))-y_2))) #theta = theta - (alpha/m)*(X'*(sigmoid(X*theta)-y))
  21. print(costFunction(theta,x,y_2,0.1))
  22. if i == num_labels:
  23. all_theta[0,:] = theta.transpose()
  24. else:
  25. all_theta[i,:] = theta.transpose()#0-10 1-1 2-2 3-3
  26. return all_theta
  27.  
  28. def costFunction(theta,x,y,lambdaa):
  29. m = y.shape[0]
  30. h = sigmoid(np.dot(x,theta))
  31. J = 1/m * ((np.dot(-y.transpose(), np.log(h))-np.dot((1-y).transpose(),np.log(1-h))))
  32. return J
  33. #J = 1/m * (-y'*log(h)-(1-y)'*log(1-h)) + lambda/(2*m)*sum(theta.^2);
  34.  
  35. def predict(x,theta):
  36. m = x.shape[0]
  37. num_labels = theta.shape[0]
  38. answers = np.zeros((m,1))
  39. #print(x.shape)
  40. #print(np.ones(x.shape[0]).shape)
  41. x = np.append(np.ones((x.shape[0],1)),x,axis=1)
  42. for i in range(m):
  43. k = np.zeros((num_labels,1))
  44. for j in range(num_labels):
  45. print("{} {}".format(theta[j:j+1,:].transpose().shape,x[i:i+1,:].shape))
  46. k[j,:] = np.dot(theta[j:j+1,:],x[i:i+1,:].transpose())
  47. answers[i,0] = np.argmax(k)
  48. answers[answers == 0] = 10
  49. return answers
  50.  
  51. def success(test_matrix_x,test_matrix_y,theta):
  52. counter = 0
  53. answers = predict(test_matrix_x,theta)
  54. for i in range(answers.shape[0]):
  55. if np.array_equal(answers[i,:], test_matrix_y[i,:]):
  56. counter+=1
  57. return counter/test_matrix_y.shape[0]
  58.  
  59. def main():
  60. f = sio.loadmat('ex3data1.mat')
  61. matrix_a = f['X']
  62. matrix_b = f['y']
  63. #print("{} {}".format(matrix_a.shape,matrix_b.shape))
  64. #test_matrix_x = np.append(matrix_a[490:500,:],matrix_a[990:1000,:],axis=0)#matrix_a[1490:1500,:],matrix_a[1990:2000,:],matrix_a[2490:2500,:],matrix_a[2990:3000,:],matrix_a[3490:3500,:],matrix_a[3990:4000,:],matrix_a[4490:4500,:],matrix_a[4990:5000,:],axis=0)
  65. #test_matrix_y = np.append(matrix_b[490:500,:],matrix_b[990:1000,:],axis=0)#,matrix_b[1490:1500,:],matrix_b[1990:2000,:],matrix_b[2490:2500,:],matrix_b[2990:3000,:],matrix_b[3490:3500,:],matrix_b[3990:4000,:],matrix_b[4490:4500,:],matrix_b[4990:5000,:],axis=0)
  66. #print(test_matrix_x.shape, test_matrix_y.shape)
  67. #rint(findingTheta(Xm,ym,4, 0.1,800))
  68. #theta = findingTheta(matrix_a,matrix_b,10,0.1,800)
  69. theta = np.array([[0.04426459]
  70. , [0.09915813]
  71. , [0.09843707]
  72. , [0.08167702]
  73. , [0.12044841]
  74. , [0.05986689]
  75. , [0.07325247]
  76. , [0.14003263]
  77. , [0.126199]
  78. , [0.04201049]])
  79. #print(success(test_matrix_x,test_matrix_y,theta))
  80. matrix1 = getMatrix("number4.png")
  81. print("Program rozpoznał cyfrę jesto to: {}".format(int(predict(matrix1,theta))))
  82. main()
  83.  
  84. import numpy as np
  85. import matplotlib.pylab as plt
  86. import matplotlib.cm as cm
  87. import scipy.misc as misc
  88. #from PIL import Image
  89.  
  90. def weightedAverage(pixel):
  91. return 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2]
  92.  
  93. def simple_treshold(im, threshold=128):
  94. return ((im > threshold) * 255).astype("uint8")
  95.  
  96. def getMatrix(path):
  97. img = plt.imread("number9.png")
  98. grey = np.zeros((img.shape[0], img.shape[1]))
  99. for i in range(len(img)):
  100. for j in range(img.shape[1]):
  101. grey[i][j] = weightedAverage(img[i][j])
  102. grey = misc.imresize(grey,size = (20,20),interp='cubic')
  103. plt.imshow(grey, cmap=cm.Greys_r)
  104. plt.show()
  105. grey.resize((400,1))
  106. return grey
  107.  
  108. #grey = simple_treshold(grey)
  109. #gray_scale_omg.thumbnail((20,20), Image.ANTIALIAS)
  110. #plt.imshow(img)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement