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


Posted in Python onAugust 08, 2018

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

下载地址:yann.lecun.com/exdb/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()

跑出的结果如下:

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

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

Python 相关文章推荐
Python中Continue语句的用法的举例详解
May 14 Python
Python ValueError: invalid literal for int() with base 10 实用解决方法
Jun 21 Python
浅谈python多线程和队列管理shell程序
Aug 04 Python
Python采用Django开发自己的博客系统
Sep 29 Python
python实现数独游戏 java简单实现数独游戏
Mar 30 Python
python实现动态数组的示例代码
Jul 15 Python
python web框架中实现原生分页
Sep 08 Python
Python实现自动访问网页的例子
Feb 21 Python
python3利用Axes3D库画3D模型图
Mar 25 Python
Python基于pyjnius库实现访问java类
Jul 31 Python
Python命令行参数argv和argparse该如何使用
Feb 08 Python
Python操作Excel的学习笔记
Feb 18 Python
python实现屏保计时器的示例代码
Aug 08 #Python
详解Python 装饰器执行顺序迷思
Aug 08 #Python
python Flask 装饰器顺序问题解决
Aug 08 #Python
Python BS4库的安装与使用详解
Aug 08 #Python
python特性语法之遍历、公共方法、引用
Aug 08 #Python
用Python shell简化开发
Aug 08 #Python
在Python中使用gRPC的方法示例
Aug 08 #Python
You might like
在PHP中操作Excel实例代码
2010/04/29 PHP
PHP序列号生成函数和字符串替换函数代码
2012/06/07 PHP
php中随机函数mt_rand()与rand()性能对比分析
2014/12/01 PHP
php中的动态调用实例分析
2015/01/07 PHP
PHP实现ASCII码与字符串相互转换的方法
2017/04/29 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
Javascript 自适应高度的Tab选项卡
2011/04/05 Javascript
jquery ajax jsonp跨域调用实例代码
2013/12/11 Javascript
js实现禁止中文输入的方法
2015/01/14 Javascript
Javascript中数组方法汇总(推荐)
2015/04/01 Javascript
Bootstrap每天必学之简单入门
2015/11/19 Javascript
浅谈js控制li标签排序问题 js调用php函数的方法
2016/10/16 Javascript
微信小程序封装http访问网络库实例代码
2017/05/24 Javascript
如何在postman中添加cookie信息步骤解析
2020/06/30 Javascript
javascript实现移动端轮播图
2020/12/09 Javascript
[06:38]DOTA2怒掀电竞风暴 2013Chinajoy
2013/07/27 DOTA
Django框架中的对象列表视图使用示例
2015/07/21 Python
用Python解决计数原理问题的方法
2016/08/04 Python
python安装cx_Oracle模块常见问题与解决方法
2017/02/21 Python
python3大文件解压和基本操作
2017/12/15 Python
Python 实现12306登录功能实例代码
2018/02/09 Python
Python tkinter的grid布局及Text动态显示方法
2018/10/11 Python
详解Python图像处理库Pillow常用使用方法
2019/09/02 Python
Python爬虫实现HTTP网络请求多种实现方式
2020/06/19 Python
如何教少儿学习Python编程
2020/07/10 Python
如何利用python发送邮件
2020/09/26 Python
Python使用paramiko连接远程服务器执行Shell命令的实现
2021/03/04 Python
Java中实现多态的机制
2015/08/09 面试题
民族团结先进个人材料
2014/02/05 职场文书
面试自我介绍演讲稿
2014/04/29 职场文书
安全施工责任书
2014/08/25 职场文书
2015年元旦演讲稿
2014/09/12 职场文书
2016大一新生入学教育心得体会
2016/01/23 职场文书
SpringBoot读取Resource下文件的4种方法
2021/07/02 Java/Android
Golang 链表的学习和使用
2022/04/19 Golang
MySQL去除密码登录告警的方法
2022/04/20 MySQL