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

本文是吴恩达机器学习课程的Python作业,涉及多分类和神经网络。首先介绍了多分类问题,包括数据集加载、可视化和向量化逻辑回归的实现,然后详细讲解了一对多分类的逻辑回归。接着,文章探讨了神经网络模型的表示和前向传播预测,用于识别手写数字。

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]), 100)
        # Get the data of random images  获取灰度图像数据
        image_data = X[indexes, :]  # shape: (100, 400)
        # Plot the figure  可视化
        fig, fig_images = plt.subplots(figsize=[8, 8], nrows=10, ncols=10,
                                       sharex=True, sharey=True)
        for row in range(10):
            for col in range
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值