# neural network(神经网络)
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_data(path, transpose=True):data = sio.loadmat(path)y = data.get('y')  # (5000,1)y = y.reshape(y.shape[0])  # make it back to column vectorX = data.get('X')  # (5000,400)if transpose:# for this dataset, you need a transpose to get the orientation rightX = np.array([im.reshape((20, 20)).T for im in X])# and I flat the image again to preserve the vector presentationX = np.array([im.reshape(400) for im in X])
return X, y
X, y = load_data('ex3data1.mat')
print(X.shape)
print(y.shape)
def plot_an_image(image):
#     """
#     image : (400,)
#     """fig, ax = plt.subplots(figsize=(1, 1))ax.matshow(image.reshape((20, 20)), cmap=matplotlib.cm.binary)plt.xticks(np.array([]))  # just get rid of ticksplt.yticks(np.array([]))
#绘图函数
pick_one = np.random.randint(0, 5000)
plot_an_image(X[pick_one, :])
plt.show()
print('this should be {}'.format(y[pick_one]))
def plot_100_image(X):""" sample 100 image and show themassume the image is squareX : (5000, 400)"""size = int(np.sqrt(X.shape[1]))# sample 100 image, reshape, reorg itsample_idx = np.random.choice(np.arange(X.shape[0]), 100)  # 100*400sample_images = X[sample_idx, :]fig, ax_array = plt.subplots(nrows=10, ncols=10, sharey=True, sharex=True, figsize=(8, 8))for r in range(10):for c in range(10):ax_array[r, c].matshow(sample_images[10 * r + c].reshape((size, size)),cmap=matplotlib.cm.binary)plt.xticks(np.array([]))plt.yticks(np.array([]))  #绘图函数,画100张图片
plot_100_image(X)
plt.show()     
raw_X, raw_y = load_data('ex3data1.mat')
print(raw_X.shape)
print(raw_y.shape)
# 准备数据
# add intercept=1 for x0
X = np.insert(raw_X, 0, values=np.ones(raw_X.shape[0]), axis=1)#插入了第一列(全部为1)
X.shape
# y have 10 categories here. 1..10, they represent digit 0 as category 10 because matlab index start at 1
# I'll ditit 0, index 0 again
y_matrix = []
for k in range(1, 11):y_matrix.append((raw_y == k).astype(int))    # 见配图 "向量化标签.png"
# last one is k==10, it's digit 0, bring it to the first position,最后一列k=10,都是0,把最后一列放到第一列
y_matrix = [y_matrix[-1]] + y_matrix[:-1]
y = np.array(y_matrix)
y.shape
# 扩展 5000*1 到 5000*10
#     比如 y=10 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]: ndarray
#     """y
# train 1 model(训练一维模型)
def cost(theta, X, y):''' cost fn is -l(theta) for you to minimize'''return np.mean(-y * np.log(sigmoid(X @ theta)) - (1 - y) * np.log(1 - sigmoid(X @ theta)))
def regularized_cost(theta, X, y, l=1):'''you don't penalize theta_0'''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
def regularized_gradient(theta, X, y, l=1):'''still, leave theta_0 alone'''theta_j1_to_n = theta[1:]regularized_theta = (l / len(X)) * theta_j1_to_n# by doing this, no offset is on theta_0regularized_term = np.concatenate([np.array([0]), regularized_theta])return gradient(theta, X, y) + regularized_term
def sigmoid(z):return 1 / (1 + np.exp(-z))
def gradient(theta, X, y):'''just 1 batch gradient'''return (1 / len(X)) * X.T @ (sigmoid(X @ theta) - y)
def logistic_regression(X, y, l=1):"""generalized logistic regressionargs:X: feature matrix, (m, n+1) # with incercept x0=1y: target vector, (m, )l: lambda constant for regularizationreturn: trained parameters"""# init thetatheta = np.zeros(X.shape[1])# train itres = opt.minimize(fun=regularized_cost,x0=theta,args=(X, y, l),method='TNC',jac=regularized_gradient,options={'disp': True})# get trained parametersfinal_theta = res.xreturn final_theta
def predict(x, theta):prob = sigmoid(x @ theta)return (prob >= 0.5).astype(int)
t0 = logistic_regression(X, y[0])
print(t0.shape)
y_pred = predict(X, t0)
print('Accuracy={}'.format(np.mean(y[0] == y_pred)))
# train k model(训练k维模型)
k_theta = np.array([logistic_regression(X, y[k]) for k in range(10)])
print(k_theta.shape)
'''# 进行预测
* think about the shape of k_theta, now you are making $X\times\theta^T$
> $(5000, 401) \times (10, 401).T = (5000, 10)$
* after that, you run sigmoid to get probabilities and for each row, you find the highest prob as the answer
'''
prob_matrix = sigmoid(X @ k_theta.T)
np.set_printoptions(suppress=True)
prob_matrix
y_pred = np.argmax(prob_matrix, axis=1)#返回沿轴axis最大值的索引,axis=1代表行
y_pred
y_answer = raw_y.copy()
y_answer[y_answer==10] = 0
print(classification_report(y_answer, y_pred))
# 神经网络模型图示
'''<img style="float: left;" src="../img/nn_model.png">
'''
def load_weight(path):data = sio.loadmat(path)return data['Theta1'], data['Theta2']
theta1, theta2 = load_weight('ex3weights.mat')
theta1.shape, theta2.shape
#因此在数据加载函数中,原始数据做了转置,然而,转置的数据与给定的参数不兼容,因为这些参数是由原始数据训练的。 所以为了应用给定的参数,我需要使用原始数据(不转置)
X, y = load_data('ex3data1.mat',transpose=False)
X = np.insert(X, 0, values=np.ones(X.shape[0]), axis=1)  # intercept
X.shape, y.shape
# feed forward prediction(前馈预测)
a1 = X
z2 = a1 @ theta1.T # (5000, 401) @ (25,401).T = (5000, 25)
z2.shape
z2 = np.insert(z2, 0, values=np.ones(z2.shape[0]), axis=1)
a2 = sigmoid(z2)
a2.shape
z3 = a2 @ theta2.T
z3.shape
a3 = sigmoid(z3)
a3
y_pred = np.argmax(a3, axis=1) + 1  # numpy is 0 base index, +1 for matlab convention,返回沿轴axis最大值的索引,axis=1代表行
y_pred.shape

吴恩达机器学习作业3.2神经网络相关推荐

  1. 吴恩达|机器学习作业4.0神经网络反向传播(BP算法)

    4.0.神经网络学习 1)题目: 在本练习中,您将实现神经网络的反向传播算法,并将其应用于手写数字识别任务.在之前的练习中,已经实现了神经网络的前馈传播,并使用Andrew Ng他们提供的权值来预测手 ...

  2. 吴恩达机器学习作业7 - K-means和PCA主成分分析(Python实现)

    吴恩达机器学习作业7 - K-means和PCA主成分分析(Python实现) Introduction 在本实验中,将实现K-means聚类算法,并将其应用于图像压缩.在第二部分实验中,将使用主成分 ...

  3. 吴恩达机器学习作业ex2-python实现

    系列文章目录 吴恩达机器学习作业ex1-python实现 吴恩达机器学习作业ex2-python实现 吴恩达机器学习作业ex3-python实现 作业说明及数据集 链接:https://pan.bai ...

  4. k均值算法python实现(吴恩达机器学习作业)

    k均值算法python实现(吴恩达机器学习作业) 题目要求 数据集 读取mat文件 K-means 实现 结果 问题 题目要求 采用K均值算法对样本进行聚类. 编写K均值算法源代码,对ex7data2 ...

  5. 第一章-机器学习简介 深度之眼_吴恩达机器学习作业训练营

    目录 专栏简介: 一,机器学习简介 1.1 机器学习定义 1.1 机器学习的重要性 1.2 应用领域 二.监督学习 三.无监督学习 四.总结 专栏简介: 本栏主要内容为吴恩达机器学习公开课的学习笔记, ...

  6. 吴恩达机器学习作业Python实现(四):神经网络(反向传播)

    吴恩达机器学习系列作业目录 1 Neural Networks 神经网络 在这个练习中,你将实现反向传播算法来学习神经网络的参数.依旧是上次预测手写数数字的例子. 1.1 Visualizing th ...

  7. 吴恩达机器学习作业Python实现(三):多类分类和前馈神经网络

    吴恩达机器学习系列作业目录 1 多类分类(多个logistic回归) 我们将扩展我们在练习2中写的logistic回归的实现,并将其应用于一对多的分类(不止两个类别). import numpy as ...

  8. 吴恩达机器学习(七)神经网络(反向传播)

    目录 0. 前言 1. 代价函数(Cost Function) 2. 反向传播(back propagation) 3. 前向传播和反向传播的结合 4. 梯度检测(gradient checking) ...

  9. 吴恩达机器学习(六)神经网络(前向传播)

    目录 0. 前言 1. 神经网络模型 2. 前向传播(forward propagation) 3. 神经网络中的多分类 学习完吴恩达老师机器学习课程的神经网络,简单的做个笔记.文中部分描述属于个人消 ...

最新文章

  1. tf.expand_dims()
  2. Entity Framework Core 2.0 使用入门
  3. SanDisk闪迪推面向VMware Virtual SAN 6的增强型闪存
  4. springside3.3.4部署小结
  5. 错误处理: socket.timeout: The read operation timed out
  6. 剧本杀,继狼人杀之后的下一个风口
  7. Ubuntu单用户修改root密码
  8. 行为模型:客户行为智能分析模型
  9. Redis进阶篇主从复制----哨兵模式
  10. 关于单体化和属性文件的说明
  11. Spring Boot(1) 入门、自动配置
  12. linux 下.bashrc和.profile的区别
  13. python基础补漏-03-函数
  14. 09. Django基础:URL反向解析
  15. scapy爬虫-Url去重
  16. 35岁以后对自己职业人生的思考及一些感悟
  17. android 9.0 开机动画,Android bootanim开机动画启动流程
  18. Navicat Premium15 注册出现No all pattern found! file already patched?
  19. 微信小程序中音频播放
  20. C语言基础课 编写程序之1.百元买鸡,公鸡一只5元,母鸡一只3元,小鸡3只一元,现将一百元要买一百只鸡,公鸡母鸡小鸡各多少只2.编程求1~200中能被2除余1或能被3除余1或能被5除余1的前10个整数

热门文章

  1. [Nginx] – 性能优化 – 配置文件优化
  2. 【Step1】【二分图匹配】poj 1274-The Perfect Stall
  3. Python结合Shell/Hadoop实现MapReduce
  4. Hi!怂程见证我开博。
  5. greedy算法/算法导论/挑战程序设计竞赛
  6. LoadRunner - 实战,转发
  7. HNU 12894 Keys dfs
  8. ubuntu命令查询版本和内核版本
  9. 游戏软件的测试方法简述
  10. DevExpress控件介绍