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同时兼容python2和python3的8个技巧分享
Jul 11 Python
Python3实现从文件中读取指定行的方法
May 22 Python
Mac 上切换Python多版本
Jun 17 Python
python如何将图片转换为字符图片
Aug 19 Python
Python3多进程 multiprocessing 模块实例详解
Jun 11 Python
python实现随机梯度下降法
Mar 24 Python
win10下python3.5.2和tensorflow安装环境搭建教程
Sep 19 Python
Python Pywavelet 小波阈值实例
Jan 09 Python
详解python selenium 爬取网易云音乐歌单名
Mar 28 Python
Python-while 计算100以内奇数和的方法
Jun 11 Python
keras slice layer 层实现方式
Jun 11 Python
python和anaconda的区别
May 06 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 模拟$_PUT实现代码
2010/03/15 PHP
php绘制一条直线的方法
2015/01/24 PHP
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
2016/02/23 PHP
添加到收藏夹代码(兼容几乎所有的浏览器)
2007/01/09 Javascript
自制的文件上传JS控件可支持IE、chrome、firefox etc
2014/04/18 Javascript
百度判断手机终端并自动跳转js代码及使用实例
2014/06/11 Javascript
jQuery模拟新浪微博首页滚动效果的方法
2015/03/11 Javascript
JS显示日历和天气的方法
2016/03/01 Javascript
Ajax的概述与实现过程
2016/11/18 Javascript
Bootstrap 手风琴菜单的实现代码
2017/01/20 Javascript
利用策略模式与装饰模式扩展JavaScript表单验证功能
2017/02/14 Javascript
令按钮悬浮在(手机)页面底部的实现方法
2017/05/02 Javascript
vue router仿天猫底部导航栏功能
2017/10/18 Javascript
bootstrapTable+ajax加载数据 refresh更新数据
2018/08/31 Javascript
js实现通过开始结束控制的计时器
2019/02/25 Javascript
vue项目中js-cookie的使用存储token操作
2020/11/13 Javascript
[03:11]不朽宝藏三外观展示
2020/09/18 DOTA
Python字符编码与函数的基本使用方法
2017/09/30 Python
用tensorflow实现弹性网络回归算法
2018/01/09 Python
python基础教程项目二之画幅好画
2018/04/02 Python
Pandas 数据框增、删、改、查、去重、抽样基本操作方法
2018/04/12 Python
python调用摄像头显示图像的实例
2018/08/03 Python
对Pandas MultiIndex(多重索引)详解
2018/11/16 Python
python 实现返回一个列表中出现次数最多的元素方法
2019/06/11 Python
python如何使用socketserver模块实现并发聊天
2019/12/14 Python
Eclipse配置python默认头过程图解
2020/04/26 Python
Pandas读取csv时如何设置列名
2020/06/02 Python
Omio荷兰:预订火车、巴士和机票
2018/11/04 全球购物
美国浴缸、水槽和水龙头购物网站:Vintage Tub & Bath
2019/11/05 全球购物
客户经理岗位职责
2013/12/08 职场文书
乡镇三项教育实施方案
2014/03/30 职场文书
小学生我的梦想演讲稿
2014/08/21 职场文书
2014年重阳节活动策划方案书
2014/09/16 职场文书
信息技术研修心得体会
2016/01/08 职场文书
Unity连接MySQL并读取表格数据的实现代码
2021/06/20 MySQL
一次SQL查询优化原理分析(900W+数据从17s到300ms)
2022/06/10 SQL Server