Ex3_机器学习_吴恩达课程作业(Python):多分类和神经网络(Multi-class Classification & Neural Networks)

Ex3_机器学习_吴恩达课程作业(Python):多分类和神经网络(Multi-class Classification & Neural Networks)

使用说明:

本文章为关于吴恩达老师在Coursera上的机器学习课程的学习笔记。

  • 本文第一部分首先介绍课程对应周次的知识回顾以及重点笔记,以及代码实现的库引入。
  • 本文第二部分包括代码实现部分中的自定义函数实现细节。
  • 本文第三部分即为与课程练习题目相对应的具体代码实现。

0. Pre-condition

This section includes some introductions of libraries.

# This file includes self-created functions used in exercise 3
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt
from scipy.io import loadmat

00. Self-created Functions

This section includes self-created functions.

  • loadData(path):读取.mat数据
    # Load data from the given file  读取数据
    def loadData(path):
        df = loadmat(path)
        X = df['X']
        y = df['y']
        return X, y
    
  • loadWeight(path):用于前置传播算法,读取神经网络各层的权重数据
    # Load weight data from the given file  读取权重数据
    def loadWeight(path):
        df = loadmat(path)
        return df['Theta1'], df['Theta2']
    
  • plotOneImage(X, y):读取并处理被压缩的灰度图像数据,可视化之
    # Randomly pick a training example and visualize it
    # 随机选取一个训练样本,并且将其可视化
    def plotOneImage(X, y):
        # Randomly pick a number ranging from 0 to the size of given training examples
        index = np.random.randint(0, X.shape[0])
        # Get the data of the random image  获取灰度图像数据
        image_data = X[index, :]
        # Reshape the vector into the gray image matrix  还原被压缩的图像数据向量为20x20数组
        image = image_data.reshape((20, 20))
        # Plot the figure  可视化
        fig, fig_image = plt.subplots(figsize=[4, 4])
        fig_image.matshow(image, cmap='gray_r')
        plt.xticks([])  # 去除图像上的刻度
        plt.yticks([])
        plt.title('Image: ' + format(y[index]))
        # print('This image should be ', format(y[index]))
        plt.show()
    
  • plot100Images(X):读取并处理100条被压缩的灰度图像数据,可视化之
    # Randomly pick 100 training examples and visualize them
    # 随机选取100个训练样本,并且将其可视化
    def plot100Images(X):
        # Randomly pick 100 numbers ranging from 0 to the size of given training examples
        indexes = np.random.choice(np.arange(X.shape[0])
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
吴恩达机器学习课程中的ex3任务是关于手写数字识别的。在这个任务中,我们使用了一个包含5000个手写数字训练示例的数据集(ex3data1.mat)。每个训练示例都是一个20×20像素的灰度图像,被展开成了一个400维的向量。这些训练示例被存储在数据矩阵X中,其中每一行代表一个手写数字图像的训练示例。 此外,训练集的标签被存储在一个5000维的向量y中。为了与Octave/MATLAB索引兼容,我们把数字零映射为值10,并将数字1至9按其自然顺序标记为1至9。 在导入数据并初始化之后,我们可以开始使用这个数据集进行手写数字识别的任务了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【吴恩达机器学习作业 ex3data1 -- 多分类逻辑回归(Python)](https://blog.csdn.net/calmdownn/article/details/125992325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [吴恩达机器学习笔记---ex3(python实现)](https://blog.csdn.net/qq_45604750/article/details/107628153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值