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 04 Python
在Python中操作时间之tzset()方法的使用教程
May 22 Python
Python序列化基础知识(json/pickle)
Oct 19 Python
简单实现python聊天程序
Apr 01 Python
详解python单元测试框架unittest
Jul 02 Python
Python3实现的旋转矩阵图像算法示例
Apr 03 Python
django-rest-framework 自定义swagger过程详解
Jul 18 Python
django创建最简单HTML页面跳转方法
Aug 16 Python
Python根据指定文件生成XML的方法
Jun 29 Python
python常量折叠基础知识点讲解
Feb 28 Python
python基础详解之if循环语句
Apr 24 Python
python3实现常见的排序算法(示例代码)
Jul 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
PHP模拟post提交数据方法汇总
2016/02/16 PHP
PHP观察者模式示例【Laravel框架中有用到】
2018/06/15 PHP
PHP中$GLOBALS与global的区别详解
2019/03/21 PHP
JavaScript在IE中“意外地调用了方法或属性访问”
2008/11/19 Javascript
JS、CSS以及img对DOMContentLoaded事件的影响
2014/08/12 Javascript
JS实现至少包含字母、大小写数字、字符的密码等级的两种方法
2015/02/03 Javascript
jQuery+css实现炫目的动态块漂移效果
2016/01/28 Javascript
jquery中$.fn和图片滚动效果实现的必备知识总结
2017/04/21 jQuery
深入浅析Node.js单线程模型
2017/07/10 Javascript
React Native 截屏组件的示例代码
2017/12/06 Javascript
mpvue性能优化实战技巧(小结)
2019/04/17 Javascript
手把手教你 CKEDITOR 4 扩展插件制作
2019/06/18 Javascript
JS常见面试试题总结【去重、遍历、闭包、继承等】
2019/08/27 Javascript
Elementui表格组件+sortablejs实现行拖拽排序的示例代码
2019/08/28 Javascript
微信小程序自定义纯净模态框(弹出框)的实例代码
2020/03/09 Javascript
[02:46]完美世界DOTA2联赛PWL DAY4集锦
2020/11/03 DOTA
分享15个最受欢迎的Python开源框架
2014/07/13 Python
Python闭包实现计数器的方法
2015/05/05 Python
Python hexstring-list-str之间的转换方法
2019/06/12 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
Django 实现图片上传和显示过程详解
2019/07/18 Python
使用Pyhton 分析酒店针孔摄像头
2020/03/04 Python
Python闭包及装饰器运行原理解析
2020/06/17 Python
HTML5 Canvas+JS控制电脑或手机上的摄像头实例
2014/05/03 HTML / CSS
如何设置Java的运行环境
2013/04/05 面试题
采购员岗位职责
2013/11/15 职场文书
服装厂厂长岗位职责
2013/12/27 职场文书
党员2014两会学习心得体会
2014/03/17 职场文书
自我推荐信格式模板
2015/03/24 职场文书
2015年试用期工作总结范文
2015/05/28 职场文书
2016年小学生新年寄语
2015/08/18 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书
教师理论学习心得体会
2016/01/21 职场文书
《颐和园》教学反思
2016/02/19 职场文书
2019行政前台转正申请书范文3篇
2019/08/15 职场文书
React 高阶组件HOC用法归纳
2021/06/13 Javascript