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连接SQLServer2000的方法详解
Apr 19 Python
Python读取MRI并显示为灰度图像实例代码
Jan 03 Python
Python 实现某个功能每隔一段时间被执行一次的功能方法
Oct 14 Python
华为校园招聘上机笔试题 扑克牌大小(python)
Apr 22 Python
详解Python 调用C# dll库最简方法
Jun 20 Python
python Django里CSRF 对应策略详解
Aug 05 Python
Python for i in range ()用法详解
Sep 18 Python
在tensorflow中设置使用某一块GPU、多GPU、CPU的操作
Feb 07 Python
python随机模块random使用方法详解
Feb 14 Python
使用Python实现微信拍一拍功能的思路代码
Jul 09 Python
使用豆瓣源来安装python中的第三方库方法
Jan 26 Python
python自动计算图像数据集的RGB均值
Jun 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用数组返回无限分类的列表数据的代码
2010/08/08 PHP
php一次性删除前台checkbox多选内容的方法
2013/09/22 PHP
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
2014/09/04 PHP
[原创]ThinkPHP让../Public在模板不解析(直接输出)的方法
2015/10/09 PHP
php实现压缩合并js的方法【附demo源码下载】
2016/09/22 PHP
对YUI扩展的Gird组件 Part-1
2007/03/10 Javascript
使用js正则控制input标签只允许输入的值
2013/07/29 Javascript
js实现简单的星级选择器提交效果适用于评论等
2013/10/18 Javascript
如何将网页表格内容导入excel
2014/02/18 Javascript
js 日期比较相关天数代码
2014/04/02 Javascript
jQuery 选择同时包含两个class的元素的实现方法
2016/06/01 Javascript
基于jQuery实现Accordion手风琴自定义插件
2020/10/13 Javascript
JavaScript解析JSON格式数据的方法示例
2017/01/24 Javascript
Bootstrap面板(Panels)的简单实现代码
2017/03/17 Javascript
微信小程序使用component自定义toast弹窗效果
2018/11/27 Javascript
vue-cli2与vue-cli3在一台电脑共存的实现方法
2019/09/25 Javascript
vue quill editor 使用富文本添加上传音频功能
2020/01/14 Javascript
微信小程序实现canvas分享朋友圈海报
2020/06/21 Javascript
js实现表格单列按字母排序
2020/08/12 Javascript
[01:20]PWL S2开团时刻第三期——团战可以输 蝙蝠必须死
2020/11/26 DOTA
python生成excel的实例代码
2017/11/08 Python
Python 3.x 判断 dict 是否包含某键值的实例讲解
2018/07/06 Python
Django框架模板的使用方法示例
2019/05/25 Python
Python3中urlencode和urldecode的用法详解
2019/07/23 Python
python扫描线填充算法详解
2020/02/19 Python
python3光学字符识别模块tesserocr与pytesseract的使用详解
2020/02/26 Python
SheIn俄罗斯:时尚女装网上商店
2017/02/28 全球购物
德国足球商店:OUTFITTER
2019/05/06 全球购物
.NET初级开发工程师面试题
2014/04/18 面试题
初中生自我鉴定
2014/02/04 职场文书
高中军训感言800字
2014/03/05 职场文书
2014向国旗敬礼网上签名活动总结
2014/09/27 职场文书
红高粱观后感
2015/06/10 职场文书
儿童诗两首教学反思
2016/02/23 职场文书
话题作文之自信作文
2019/11/15 职场文书
react中useState使用:如何实现在当前表格直接更改数据
2022/08/05 Javascript