机器学习笔记 -吴恩达(第七章:神经网络-手写数字识别,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))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值