机器学习笔记 -吴恩达(第七章:神经网络-手写数字识别,python实现 附源码)

(1)数据预处理

import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report

#加载权重值

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

theta1, theta2 = load_weight('ex3weights.mat')

theta1.shape, theta2.shape

#((25, 401), (10, 26))

X, y = load_data('ex3data1.mat',transpose=False)

X = np.insert(X, 0, values=np.ones(X.shape[0]), axis=1)  # 偏置项

X.shape, y.shape

#

((5000, 401), (5000,))

(2)前向传导

a1 = X

z2 = a1 @ theta1.T # (5000, 401) @ (25,401).T = (5000, 25)   401为输入层节点数,25为隐藏层节点数,25个特征提取
z2.shape

z2 = np.insert(z2, 0, values=np.ones(z2.shape[0]), axis=1)  #偏置项,增加隐藏层一个输入

a2 = sigmoid(z2)
a2.shape

#

(5000, 26)

z3 = a2 @ theta2.T
z3.shape

# (5000, 10)

#输出为分类个数,再次提取特征,10个分类

a3 = sigmoid(z3)
a3

(3)预测

y_pred = np.argmax(a3, axis=1) + 1  # numpy is 0 base index, +1 for matlab convention,返回沿轴axis最大值的索引,axis=1代表行
y_pred.shape

(5000,)

(4) 评估

print(classification_report(y, y_pred))

你可能感兴趣的:(机器学习-吴恩达)