欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

吴恩达 机器学习课程 coursera 第三次编程作业(Neural Network) python实现

程序员文章站 2022-07-13 08:51:17
...

本文是吴恩达机器学习课程的第三次编程作业:Neural Network ,用python实现。

 

ex3.py为主程序入口。

作业文件和训练集数据下载地址:https://github.com/toanoyx/MachineLearning-AndrewNg-coursera-python/tree/master/ex3%20Neural%20Network

 

下文是文件的源代码:

ex3.py

from loadData import *
from displayData import *
from logisticRegression import *
from predict import *
from loadWeight import *
from sklearn.metrics import classification_report


""" 第1部分 加载数据集 """
X, y = loadData('ex3data1.mat')


""" 第2部分 可视化 """
displayData(X)
plt.show()

raw_X, raw_y = loadData('ex3data1.mat')


""" 第3部分 向量化逻辑回归 """
X = np.insert(raw_X, 0, values=np.ones(raw_X.shape[0]), axis=1)
y_matrix = []

for k in range(1, 11):
    y_matrix.append((raw_y == k).astype(int))

y_matrix = [y_matrix[-1]] + y_matrix[:-1]
y = np.array(y_matrix)

t0 = logisticRegression(X, y[0])
print(t0.shape)
y_pred = predict(X, t0)
print('Accuracy={}'.format(np.mean(y[0] == y_pred)))

k_theta = np.array([logisticRegression(X, y[k]) for k in range(10)])
print(k_theta.shape)

prob_matrix = sigmoid(X @ k_theta.T)
np.set_printoptions(suppress=True)
y_pred = np.argmax(prob_matrix, axis=1)
y_answer = raw_y.copy()
y_answer[y_answer==10] = 0
print(classification_report(y_answer, y_pred))

""" 第4部分 神经网络模型 """
theta1, theta2 = loadWeight('ex3weights.mat')
X, y = loadData('ex3data1.mat', transpose=False)
X = np.insert(X, 0, values=np.ones(X.shape[0]), axis=1)
a1 = X
z2 = a1 @ theta1.T
z2 = np.insert(z2, 0, values=np.ones(z2.shape[0]), axis=1)
a2 = sigmoid(z2)
z3 = a2 @ theta2.T
a3 = sigmoid(z3)
y_pred = np.argmax(a3, axis=1) + 1
print(classification_report(y, y_pred))

 

loadData.py

import scipy.io as sio
import numpy as np


def loadData(path, transpose=True):
    data = sio.loadmat(path)
    y = data.get('y')
    y = y.reshape(y.shape[0])
    X = data.get('X')

    if transpose:
        X = np.array([im.reshape((20, 20)).T for im in X])
        X = np.array([im.reshape(400) for im in X])

    return X, y

 

lrCostFunction.py

import numpy as np
from sigmoid import *


def cost(theta, X, y):
    return np.mean(-y * np.log(sigmoid(X @ theta)) - (1 - y) * np.log(1 - sigmoid(X @ theta)))


def lrCostFunction(theta, X, y, l=1):
    theta_j1_to_n = theta[1:]
    regularized_term = (l / (2 * len(X))) * np.power(theta_j1_to_n, 2).sum()

    return cost(theta, X, y) + regularized_term

 

lrGradient.py

import numpy as np
from sigmoid import *


def gradient(theta, X, y):
    return (1 / len(X)) * X.T @ (sigmoid(X @ theta) - y)


def lrGradient(theta, X, y, l=1):
    theta_j1_to_n = theta[1:]
    regularized_theta = (l / len(X)) * theta_j1_to_n

    regularized_term = np.concatenate([np.array([0]), regularized_theta])

    return gradient(theta, X, y) + regularized_term

 

logisticRegression.py

import numpy as np
import scipy.optimize as opt
from lrCostFunction import *
from lrGradient import *


def logisticRegression(X, y, l=1):
    theta = np.zeros(X.shape[1])

    res = opt.minimize(fun=lrCostFunction,
                       x0=theta,
                       args=(X, y, l),
                       method='TNC',
                       jac=lrGradient,
                       options={'disp': True})
    final_theta = res.x

    return final_theta

 

predict.py

from sigmoid import *


def predict(x, theta):
    prob = sigmoid(x @ theta)
    return (prob >= 0.5).astype(int)

 

sigmoid.py

import numpy as np


def sigmoid(z):
    return 1 / (1 + np.exp(-z))

 

loadWeight.py

import scipy.io as sio


def loadWeight(path):
    data = sio.loadmat(path)
    return data['Theta1'], data['Theta2']