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中的字典来处理索引统计的方法
May 05 Python
解决python matplotlib imshow无法显示的问题
May 24 Python
python打包生成的exe文件运行时提示缺少模块的解决方法
Oct 31 Python
Python从Excel中读取日期一列的方法
Nov 28 Python
图文详解Django使用Pycharm连接MySQL数据库
Aug 09 Python
用sqlalchemy构建Django连接池的实例
Aug 29 Python
python验证码图片处理(二值化)
Nov 01 Python
用python拟合等角螺线的实现示例
Dec 27 Python
python实现遍历文件夹图片并重命名
Mar 23 Python
Django跨域请求原理及实现代码
Nov 14 Python
详解Python中如何将数据存储为json格式的文件
Nov 18 Python
20行代码教你用python给证件照换底色的方法示例
Feb 05 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
德生S2000南麂列岛台湾FM收听记录
2021/03/02 无线电
PHP 输出缓冲控制(Output Control)详解
2016/08/25 PHP
php根据年月获取当月天数及日期数组的方法
2016/11/30 PHP
详解Yii2.0使用AR联表查询实例
2017/06/16 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
2017/11/10 PHP
对联广告js flash激活
2006/10/19 Javascript
javascript 播放器 控制
2007/01/22 Javascript
js 强制弹出窗口代码研究-又一款代码
2010/03/20 Javascript
jquery 笔记 事件
2011/11/02 Javascript
jquery.post用法之type设置问题
2014/02/24 Javascript
js判断字符长度及中英文数字等
2014/03/19 Javascript
什么是cookie?js手动创建和存储cookie
2014/05/27 Javascript
node.js中的fs.truncateSync方法使用说明
2014/12/15 Javascript
javascript表单验证大全
2015/08/12 Javascript
JS简单循环遍历json数组的方法
2016/04/22 Javascript
分享JavaScript监听全部Ajax请求事件的方法
2016/08/28 Javascript
vue.js入门教程之基础语法小结
2016/09/01 Javascript
jquery事件绑定解绑机制源码解析
2016/09/19 Javascript
让DIV的滚动条自动滚动到最底部的3种方法(推荐)
2016/09/24 Javascript
实现点击下箭头变上箭头来回切换的两种方法【推荐】
2016/12/14 Javascript
AngularJS框架的ng-app指令与自动加载实现方法分析
2017/01/04 Javascript
在bootstrap中实现轮播图实例代码
2017/06/11 Javascript
d3.js实现自定义多y轴折线图的示例代码
2018/05/30 Javascript
layer扩展打开/关闭动画的方法
2019/09/23 Javascript
微信小程序中网络请求缓存的解决方法
2019/12/29 Javascript
javascript中的offsetWidth、clientWidth、innerWidth及相关属性方法
2020/05/14 Javascript
vue+高德地图实现地图搜索及点击定位操作
2020/09/09 Javascript
Python调用C语言的方法【基于ctypes模块】
2018/01/22 Python
Python简直是万能的,这5大主要用途你一定要知道!(推荐)
2019/04/03 Python
python防止随意修改类属性的实现方法
2019/08/21 Python
2014年应届大学生自我评价
2014/01/09 职场文书
档案保密承诺书
2014/06/03 职场文书
班级光棍节联谊会策划书
2014/10/10 职场文书
2014教师年度工作总结
2014/11/10 职场文书
致毕业季:你如何做好自己的职业生涯规划书?
2019/07/01 职场文书
MySQL中存储时间的最佳实践指南
2021/07/01 MySQL