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 文件操作实现代码
Oct 07 Python
python smtplib模块发送SSL/TLS安全邮件实例
Apr 08 Python
python制作最美应用的爬虫
Oct 28 Python
Python解析并读取PDF文件内容的方法
May 08 Python
TensorFlow实现简单卷积神经网络
May 24 Python
Linux系统(CentOS)下python2.7.10安装
Sep 26 Python
python实现两张图片的像素融合
Feb 23 Python
python elasticsearch从创建索引到写入数据的全过程
Aug 04 Python
使用Pyhton集合set()实现成果查漏的例子
Nov 24 Python
python3 实现口罩抽签的功能
Mar 11 Python
python中wx模块的具体使用方法
May 15 Python
python获取linux系统信息的三种方法
Oct 14 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/26 PHP
在PHP中使用X-SendFile头让文件下载更快
2014/06/01 PHP
php浏览历史记录的方法
2015/03/10 PHP
php版微信开发Token验证失败或请求URL超时问题的解决方法
2016/09/23 PHP
详解PHP函数 strip_tags 处理字符串缺陷bug
2017/06/11 PHP
PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
2017/08/10 PHP
php 后端实现JWT认证方法示例
2018/09/04 PHP
PHP单例模式实例分析【防继承,防克隆操作】
2019/05/22 PHP
laravel5.2表单验证,并显示错误信息的实例
2019/09/29 PHP
解决laravel5中auth用户登录其他页面获取不到登录信息的问题
2019/10/08 PHP
JavaScript 基于原型的对象(创建、调用)
2009/10/16 Javascript
关于textarea提交的内容无法换行的解决办法
2013/04/09 Javascript
获取下拉列表框的值是数组,split,$.inArray示例
2013/11/13 Javascript
jQuery显示和隐藏 常用的状态判断方法
2015/01/29 Javascript
JavaScript让网页出现渐隐渐显背景颜色的方法
2015/04/21 Javascript
jQuery获取Table某列的值(推荐)
2017/03/03 Javascript
Angularjs中使用指令绑定点击事件的方法
2017/03/30 Javascript
Vue的土著指令和自定义指令实例详解
2018/02/04 Javascript
JS基于Location实现访问Url、重定向及刷新页面的方法分析
2018/12/03 Javascript
详解vue的数据劫持以及操作数组的坑
2019/04/18 Javascript
jQuery+ajax实现批量删除功能完整示例
2019/06/06 jQuery
JavaScript 函数用法详解【函数定义、参数、绑定、作用域、闭包等】
2020/05/12 Javascript
Python使用爬虫爬取静态网页图片的方法详解
2018/06/05 Python
Python Django切换MySQL数据库实例详解
2019/07/16 Python
python [:3] 实现提取数组中的数
2019/11/27 Python
Python2手动安装更新pip过程实例解析
2020/07/16 Python
Amcal中文官网:澳洲综合性连锁药房
2019/03/28 全球购物
学生打架检讨书
2014/02/14 职场文书
搞笑征婚广告词
2014/03/17 职场文书
文员转正自我鉴定怎么写
2014/09/29 职场文书
2015年女职工工作总结
2015/05/15 职场文书
退伍军人感言
2015/08/01 职场文书
html5移动端禁止长按图片保存的实现
2021/04/20 HTML / CSS
详解MySql中InnoDB存储引擎中的各种锁
2022/02/12 MySQL
Nginx设置HTTPS的方法步骤 443证书配置方法
2022/03/21 Servers
python绘制云雨图raincloud plot
2022/08/05 Python