Week4: Logistic Regression, Multi-Classification

import numpy as np
import scipy.optimize as opt
import scipy.io
import random
import matplotlib.pyplot as plt
import matplotlib.cm as cm # Used to display images in a specific colormap

# Importing .mat data
data = scipy.io.loadmat(ex3data1.mat)
X, y = data[X], data[y]
m, n = X.shape
X = np.append(np.ones((m, 1)), X, axis=1)

# Helper function to plot data
def getDatumImg(row):
"""
Function that is handed a single np array with shape 1x400,
crates an image object from it, and returns it
"""
width, height = 20, 20
square = row[1:].reshape(width, height)
return square.T

# Plotting data
def displayData(X):
"""
Function that picks 100 random rows from X, creates a 20x20 image from each,
then stitches them together into a 10x10 grid of images, and shows it.
"""
width, height = 20, 20
nrows, ncols = 10, 10
indices_to_display = random.sample(range(0, X.shape[0]), nrows * ncols)
big_picture = np.zeros((height * nrows, width * ncols))
irow, icol = 0, 0
for idx in indices_to_display:
if icol == ncols:
irow += 1
icol = 0
iimg = getDatumImg(X[idx])
big_picture[irow * height:irow * height + iimg.shape[0], icol * width:icol * width + iimg.shape[1]] = iimg
icol += 1
plt.imshow(big_picture, cmap=cm.Greys_r)
plt.axis(off)
plt.show()

displayData(X)

# Define cost function
def sigmoid(X):
return 1/(1 + np.exp(-X))

def cost_function_reg(theta, X, y, lambda_reg):
m, n = np.shape(X)
theta = theta.reshape(n, 1)
h = sigmoid(X.dot(theta))
initial_theta = theta[0]
theta[0] = 0
j = (1 / m) * (-y.T.dot(np.log(h)) - (1 - y).T.dot(np.log(1 - h))) + lambda_reg / (2*m) * theta.T.dot(theta)
grad = (1/m)*X.T.dot(h-y) + lambda_reg /m * theta
theta[0] = initial_theta
return j.flatten(), grad.flatten()

# Fit the theta using one_vs_all method
def one_vs_all(X, y, num_labels, lambda_reg):
m, n = X.shape
all_theta = np.zeros((n, num_labels))
for i in range(num_labels):
initial_theta = np.zeros(n)
find_theta = opt.minimize(fun=cost_function_reg, method=CG, jac=True, x0=initial_theta, args=(X, y == i + 1, lambda_reg), options={maxiter:50, disp:False}).x
all_theta[:, i] = find_theta
return all_theta

# Classification function
def predict_one_vs_all(theta, X):
ps = sigmoid(X.dot(theta))
p = ps.argmax(axis = 1) + 1
return p

# Test model accuracy using training set
num_labels = 10
lambda_reg = 0.1
all_theta = one_vs_all(X, y, num_labels, lambda_reg) # get the fitted optimal theta
print(Training set accuracy when using logistic regression is:
)
predict = predict_one_vs_all(all_theta, X)
print(np.mean((predict == y.flatten()).astype(int))*100)

Week4: Neural Network, Prediction

import numpy as np
import functions as func
import scipy.io

# Importing data
dataset = scipy.io.loadmat(ex3data1.mat)
X, y = dataset[X], dataset[y]
m, n = X.shape
X = np.append(np.ones((m, 1)), X, axis=1)

weights = scipy.io.loadmat(ex3weights.mat)
theta1, theta2 = weights[Theta1], weights[Theta2]

# Define predicting function
def sigmoid(X):
return 1/(1 + np.exp(-X))

def predict_nn(theta1, theta2, X):
hidden_layer = sigmoid(X.dot(theta1.T))
m, n = hidden_layer.shape
hidden_layer = np.append(np.ones((m, 1)), hidden_layer, axis=1)
output_layer = sigmoid(hidden_layer.dot(theta2.T))
predict = output_layer.argmax(axis=1) + 1
return predict

# Predict the result using trained theta provided
predict_nn = func.predict_nn(theta1, theta2, X)
print(Training set accuracy when using neural network is:
)
print(np.mean((predict_nn == y.flatten()).astype(int))*100)

推荐阅读:

相关文章