Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import scipy.io as sio
- import scipy.misc as misc
- import matplotlib.pylab as plt
- import matplotlib.cm as cm
- from PIL import Image
- from imageHandler import getMatrix
- def sigmoid(x):
- return 1/(1 +np.exp(-x))
- def findingTheta(x, y, num_labels, alpha, num_iterations):
- n = (x.shape[1])
- m = y.shape[0]
- x = np.append(np.ones((x.shape[0],1)),x,axis=1)
- all_theta = np.zeros((num_labels,n+1)) # all_theta = zeros(num_labels, n+1)
- theta = np.zeros((n+1,1))#nitial_theta = zeros(size(x, 2))
- for i in range(1,num_labels+1):#sprawdzic czy 0 tez jest liczba
- y_2 = (y==i).astype(int)
- for j in range(num_iterations):
- theta = theta-(alpha/m)*(np.dot(x.transpose(),(sigmoid(np.dot(x,theta))-y_2))) #theta = theta - (alpha/m)*(X'*(sigmoid(X*theta)-y))
- print(costFunction(theta,x,y_2,0.1))
- if i == num_labels:
- all_theta[0,:] = theta.transpose()
- else:
- all_theta[i,:] = theta.transpose()#0-10 1-1 2-2 3-3
- return all_theta
- def costFunction(theta,x,y,lambdaa):
- m = y.shape[0]
- h = sigmoid(np.dot(x,theta))
- J = 1/m * ((np.dot(-y.transpose(), np.log(h))-np.dot((1-y).transpose(),np.log(1-h))))
- return J
- #J = 1/m * (-y'*log(h)-(1-y)'*log(1-h)) + lambda/(2*m)*sum(theta.^2);
- def predict(x,theta):
- m = x.shape[0]
- num_labels = theta.shape[0]
- answers = np.zeros((m,1))
- #print(x.shape)
- #print(np.ones(x.shape[0]).shape)
- x = np.append(np.ones((x.shape[0],1)),x,axis=1)
- for i in range(m):
- k = np.zeros((num_labels,1))
- for j in range(num_labels):
- print("{} {}".format(theta[j:j+1,:].transpose().shape,x[i:i+1,:].shape))
- k[j,:] = np.dot(theta[j:j+1,:],x[i:i+1,:].transpose())
- answers[i,0] = np.argmax(k)
- answers[answers == 0] = 10
- return answers
- def success(test_matrix_x,test_matrix_y,theta):
- counter = 0
- answers = predict(test_matrix_x,theta)
- for i in range(answers.shape[0]):
- if np.array_equal(answers[i,:], test_matrix_y[i,:]):
- counter+=1
- return counter/test_matrix_y.shape[0]
- def main():
- f = sio.loadmat('ex3data1.mat')
- matrix_a = f['X']
- matrix_b = f['y']
- #print("{} {}".format(matrix_a.shape,matrix_b.shape))
- #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)
- #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)
- #print(test_matrix_x.shape, test_matrix_y.shape)
- #rint(findingTheta(Xm,ym,4, 0.1,800))
- #theta = findingTheta(matrix_a,matrix_b,10,0.1,800)
- theta = np.array([[0.04426459]
- , [0.09915813]
- , [0.09843707]
- , [0.08167702]
- , [0.12044841]
- , [0.05986689]
- , [0.07325247]
- , [0.14003263]
- , [0.126199]
- , [0.04201049]])
- #print(success(test_matrix_x,test_matrix_y,theta))
- matrix1 = getMatrix("number4.png")
- print("Program rozpoznał cyfrę jesto to: {}".format(int(predict(matrix1,theta))))
- main()
- import numpy as np
- import matplotlib.pylab as plt
- import matplotlib.cm as cm
- import scipy.misc as misc
- #from PIL import Image
- def weightedAverage(pixel):
- return 0.299 * pixel[0] + 0.587 * pixel[1] + 0.114 * pixel[2]
- def simple_treshold(im, threshold=128):
- return ((im > threshold) * 255).astype("uint8")
- def getMatrix(path):
- img = plt.imread("number9.png")
- grey = np.zeros((img.shape[0], img.shape[1]))
- for i in range(len(img)):
- for j in range(img.shape[1]):
- grey[i][j] = weightedAverage(img[i][j])
- grey = misc.imresize(grey,size = (20,20),interp='cubic')
- plt.imshow(grey, cmap=cm.Greys_r)
- plt.show()
- grey.resize((400,1))
- return grey
- #grey = simple_treshold(grey)
- #gray_scale_omg.thumbnail((20,20), Image.ANTIALIAS)
- #plt.imshow(img)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement