热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python调用数据集mnist_pythonMNIST手写识别数据调用API的方法

MNIST数据集比较小,一般入门机器学习都会采用这个数据集来训练有4个有用的文件:train-images-idx3-ubyte:trainingset

MNIST数据集比较小,一般入门机器学习都会采用这个数据集来训练

有4个有用的文件:

train-images-idx3-ubyte: training set images

train-labels-idx1-ubyte: training set labels

t10k-images-idx3-ubyte: test set images

t10k-labels-idx1-ubyte: test set labels

The training set contains 60000 examples, and the test set 10000 examples. 数据集存储是用binary file存储的,黑白图片。

下面给出load数据集的代码:

import os

import struct

import numpy as np

import matplotlib.pyplot as plt

def load_mnist():

'''

Load mnist data

http://yann.lecun.com/exdb/mnist/

60000 training examples

10000 test sets

Arguments:

kind: 'train' or 'test', string charater input with a default value 'train'

Return:

xxx_images: n*m array, n is the sample count, m is the feature number which is 28*28

xxx_labels: class labels for each image, (0-9)

'''

root_path = '/home/cc/deep_learning/data_sets/mnist'

train_labels_path = os.path.join(root_path, 'train-labels.idx1-ubyte')

train_images_path = os.path.join(root_path, 'train-images.idx3-ubyte')

test_labels_path = os.path.join(root_path, 't10k-labels.idx1-ubyte')

test_images_path = os.path.join(root_path, 't10k-images.idx3-ubyte')

with open(train_labels_path, 'rb') as lpath:

# '>' denotes bigedian

# 'I' denotes unsigned char

magic, n = struct.unpack('>II', lpath.read(8))

#loaded = np.fromfile(lpath, dtype = np.uint8)

train_labels = np.fromfile(lpath, dtype = np.uint8).astype(np.float)

with open(train_images_path, 'rb') as ipath:

magic, num, rows, cols = struct.unpack('>IIII', ipath.read(16))

loaded = np.fromfile(train_images_path, dtype = np.uint8)

# images start from the 16th bytes

train_images = loaded[16:].reshape(len(train_labels), 784).astype(np.float)

with open(test_labels_path, 'rb') as lpath:

# '>' denotes bigedian

# 'I' denotes unsigned char

magic, n = struct.unpack('>II', lpath.read(8))

#loaded = np.fromfile(lpath, dtype = np.uint8)

test_labels = np.fromfile(lpath, dtype = np.uint8).astype(np.float)

with open(test_images_path, 'rb') as ipath:

magic, num, rows, cols = struct.unpack('>IIII', ipath.read(16))

loaded = np.fromfile(test_images_path, dtype = np.uint8)

# images start from the 16th bytes

test_images = loaded[16:].reshape(len(test_labels), 784)

return train_images, train_labels, test_images, test_labels

再看看图片集是什么样的:

def test_mnist_data():

'''

Just to check the data

Argument:

none

Return:

none

'''

train_images, train_labels, test_images, test_labels = load_mnist()

fig, ax = plt.subplots(nrows = 2, ncols = 5, sharex = True, sharey = True)

ax =ax.flatten()

for i in range(10):

img = train_images[i][:].reshape(28, 28)

ax[i].imshow(img, cmap = 'Greys', interpolation = 'nearest')

print('corresponding labels = %d' %train_labels[i])

if __name__ == '__main__':

test_mnist_data()

跑出的结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。



推荐阅读
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
author-avatar
版中凌菱
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有