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创建xml的方法
Mar 10 Python
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 Python
Python实现将16进制字符串转化为ascii字符的方法分析
Jul 21 Python
50行Python代码实现人脸检测功能
Jan 23 Python
django之跨表查询及添加记录的示例代码
Oct 16 Python
详解Python正则表达式re模块
Mar 19 Python
python实现动态数组的示例代码
Jul 15 Python
python 设置xlabel,ylabel 坐标轴字体大小,字体类型
Jul 23 Python
python+selenium 鼠标事件操作方法
Aug 24 Python
Python tkinter实现简单加法计算器代码实例
May 13 Python
六种酷炫Python运行进度条效果的实现代码
Jul 17 Python
python 爬虫之selenium可视化爬虫的实现
Dec 04 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
ThinkPHP 整合Bootstrap Ajax分页样式
2016/12/23 PHP
Aster vs KG BO3 第二场2.18
2021/03/10 DOTA
一段好玩的JavaScript代码
2006/12/01 Javascript
jquery 插件 人性化的消息显示
2008/01/21 Javascript
JSON扫盲帖 JSON.as类教程
2009/02/16 Javascript
js 数值项目的格式化函数代码
2010/05/14 Javascript
jquery 实现checkbox全选,反选,全不选等功能代码(奇数)
2012/10/24 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(一)让静态人物动起来
2013/01/23 Javascript
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
javascript中checkbox使用方法简单实例演示
2015/11/17 Javascript
jQuery模拟物体自由落体运动(附演示与demo源码下载)
2016/01/21 Javascript
AngularJS入门教程之静态模板详解
2016/08/18 Javascript
jQuery包裹节点用法完整示例
2016/09/13 Javascript
基于jQuery实现Tabs选项卡自定义插件
2016/11/21 Javascript
Javacript中自定义的map.js  的方法
2017/11/26 Javascript
在vue中使用css modules替代scroped的方法
2018/03/10 Javascript
讲解vue-router之什么是编程式路由
2018/05/28 Javascript
深入分析element ScrollBar滚动组件源码
2019/01/22 Javascript
微信小程序利用Canvas绘制图片和竖排文字详解
2019/06/25 Javascript
使用微信SDK自定义分享的方法
2019/07/03 Javascript
Vue el-autocomplete远程搜索下拉框并实现自动填充功能(推荐)
2019/10/25 Javascript
解决VueCil代理本地proxytable无效报错404的问题
2020/11/07 Javascript
[01:00:52]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第一场
2018/04/05 DOTA
Python 时间操作例子和时间格式化参数小结
2014/04/24 Python
使用Python的内建模块collections的教程
2015/04/28 Python
Ubuntu下安装PyV8
2016/03/13 Python
Python+matplotlib实现华丽的文本框演示代码
2018/01/22 Python
Tesserocr库的正确安装方式
2018/10/19 Python
Python获取当前脚本文件夹(Script)的绝对路径方法代码
2019/08/27 Python
Django ORM判断查询结果是否为空,判断django中的orm为空实例
2020/07/09 Python
Python jieba库分词模式实例用法
2021/01/13 Python
Html5实现iPhone开机界面示例代码
2013/06/30 HTML / CSS
COS美国官网:知名服装品牌
2019/04/08 全球购物
公司JAVA开发面试题
2015/04/02 面试题
中学门卫岗位职责
2013/12/26 职场文书
2014年学校安全工作总结
2014/11/13 职场文书