Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import scipy.io
- import matplotlib.pyplot as plt
- # load data
- data = scipy.io.loadmat('machine-learning-ex3\ex3\ex3data1')
- # load weights
- weights = scipy.io.loadmat('machine-learning-ex3\ex3\ex3weights')
- theta1 = weights['Theta1']
- theta2 = weights['Theta2']
- print('Data shape:')
- print(data['X'].shape)
- print(data['y'].shape)
- # diplay data
- idx = np.random.randint(0, data['X'].shape[0], 100)
- def displayData(data, m, n):
- fig, ax = plt.subplots(m, n)
- for i in range(m):
- for j in range(n):
- ax[i,j].imshow(data['X'][idx[m*i + j]].reshape(20,20))
- #displayData(data, 10, 10)
- def sigmoid(z):
- return 1 / (1 + np.exp(-z))
- # One-hot encoder
- #from sklearn.preprocessing import OneHotEncoder
- #encoder = OneHotEncoder(sparse=False)
- #y_onehot = encoder.fit_transform(data['y'])
- def forward_propagation(X, theta1, theta2):
- m = X.shape[0]
- a1 = np.insert(X, 0, values=np.ones(m), axis=1)
- z2 = a1.dot(theta1.T)
- a2 = np.insert(sigmoid(z2), 0, values=np.ones(m), axis=1)
- z3 = a2.dot(theta2.T)
- h = sigmoid(z3)
- return h
- def predict(h):
- m = h.shape[0]
- n = h.shape[1]
- print(m,n)
- ret = np.zeros((m,n))
- for i in range(m):
- for j in range(n):
- if h[i][j] >= 0.5:
- ret[i][j] = 1
- else:
- ret[i][j] = 0
- return ret
- h = forward_propagation(data['X'], theta1, theta2)
- y_pred = np.array(np.argmax(h, axis=1) + 1)
- correct = [1 if a == b else 0 for (a, b) in zip(y_pred, data['y'])]
- accuracy = (sum(map(int, correct)) / float(len(correct)))
- print('accuracy = {0}%'.format(accuracy * 100))
Advertisement
Add Comment
Please, Sign In to add comment