import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
import scipy.optimize as opt
data = loadmat('ex3data1.mat')
weight = loadmat('ex3weights')
X = data['X']
Y = data['y']
Theta1 = weight['Theta1']
Theta2 = weight['Theta2']
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):
m, n = X.shape
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):
m, n = X.shape
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 feedforward_propagation(theta,X,Y):
m, n = X.shape
ones = np.matrix(np.ones((m, 1)))
X = np.column_stack([ones, X])
h = hypothesis(theta,X)
return h
hidden_layer_in = feedforward_propagation(Theta1,X,Y)
output_layer_in = feedforward_propagation(Theta2,hidden_layer_in,Y)
m, n = output_layer_in.shape
def predict(X):
p = np.zeros(m)
p = np.argmax(X,axis=1)
return p+1
y_pre = predict(output_layer_in)
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(output_layer_in)
print(accuracy)
# 0.9752