import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
import scipy.optimize as opt
data = loadmat('ex3data1.mat')
X = data['X']
Y = data['y']
m,n = X.shape
sample_index = np.random.permutation(range(m))
sample_img = X[sample_index[0:100],:]
#不會寫。。
def visual(data):
m, n = data.shape
example_width = np.int(np.round(np.sqrt(n)))
example_height = np.int((n / example_width))
display_rows = np.int(np.floor(np.sqrt(m)))
display_cols = np.int(np.ceil(m / display_rows))
pad = 1
display_array = - np.ones((pad + display_rows * (example_height + pad),
pad + display_cols * (example_width + pad)))
curr_ex = 0
for j in np.arange(display_rows):
for i in np.arange(display_cols):
if curr_ex > m:
break
max_val = np.max(np.abs(X[curr_ex, :]))
display_array[pad + j * (example_height + pad) + np.arange(example_height),
pad + i * (example_width + pad) + np.arange(example_width)[:, np.newaxis]] = \
data[curr_ex].reshape((example_height, example_width)) / max_val
curr_ex = curr_ex + 1
if curr_ex > m:
break
plt.figure()
plt.imshow(display_array, cmap='gray', extent=[-1, 1, -1, 1])
plt.axis('off')
# plt.show()
# visual(sample_img)
def sigmoid(z):
return 1/(1+np.exp(-z))
def hypothesis(theta,X):
X = np.matrix(X)
theta = np.matrix(theta)
theta = theta.T
h = X.dot(theta)
return sigmoid(h)
def cost(theta,X,Y,lambd):
h = hypothesis(theta,X)
Y = np.matrix(Y)
j = np.sum(np.multiply(-Y, np.log(h)) - np.multiply((1 - Y), np.log(1-h)))/len(X)
_theta = theta.copy()
_theta[0] = 0
penalty = (_theta).dot(_theta)*lambd/(2*m)
return j + penalty
def gradient(theta,X,Y,lambd):
h = hypothesis(theta, X)
_theta = theta.copy()
_theta[0] = 0
_theta = _theta.reshape((_theta.shape[0], 1))
partial_j = ((X.T).dot(h-Y))/ len(X)
return partial_j + lambd*_theta/len(X)
def one_all_classification(X,Y,k=10):
final_theta = np.zeros((k,n+1))
theta = np.zeros( n+1)
ones = np.matrix(np.ones((m, 1)))
classX = np.column_stack([ones, X])
for i in range(1,k+1):
classY = np.array([1 if i == lable else 0 for lable in Y ])
classY = classY.reshape(-1,1)
res = opt.minimize(fun=cost, x0=theta, args=(classX, classY, 1), method='tnc', jac=gradient)
final_theta[i-1,:] = res.x
return final_theta
def predict(theta,X):
p = np.zeros(m)
ones = np.matrix(np.ones((m, 1)))
X = np.column_stack([ones, X])
p = np.argmax(hypothesis(theta,X),axis=1)
return p+1
y_pre = predict(one_all_classification(X,Y,k=10),X)
correct = np.ones(m)
for i in range(m):
if y_pre[i]==Y[i]:
correct[i] = 1
else:
correct[i] = 0
accuracy = sum(correct) / len(X)
print(accuracy)
# 0.9446