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实现的使用telnet登陆聊天室实例
Jun 17 Python
python简单实现操作Mysql数据库
Jan 29 Python
python获取代理IP的实例分享
May 07 Python
Python闭包和装饰器用法实例详解
May 22 Python
使用Python画股票的K线图的方法步骤
Jun 28 Python
python创建ArcGIS shape文件的实现
Dec 06 Python
Django后端发送小程序微信模板消息示例(服务通知)
Dec 17 Python
Python try except异常捕获机制原理解析
Apr 18 Python
Python 为什么推荐蛇形命名法原因浅析
Jun 18 Python
什么是python的函数体
Jun 19 Python
浅谈python锁与死锁问题
Aug 14 Python
python中的None与NULL用法说明
May 25 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 数组遍历顺序理解
2009/09/09 PHP
使用phpQuery采集网页的方法
2013/11/13 PHP
javascript 原型继承介绍
2011/08/30 Javascript
用js实现输入提示(自动完成)的实例代码
2013/06/14 Javascript
jQuery 鼠标经过(hover)事件的延时处理示例
2014/04/14 Javascript
jQuery中[attribute!=value]选择器用法实例
2014/12/31 Javascript
javascript禁止访客复制网页内容的实现代码
2015/08/05 Javascript
jQuery滚动加载图片实现原理
2015/12/14 Javascript
AngularJS基础 ng-mouseenter 指令示例代码
2016/08/02 Javascript
Bootstrap模态框禁用空白处点击关闭
2016/10/20 Javascript
使用纯JS代码判断字符串中有多少汉字的实现方法(超简单实用)
2016/11/12 Javascript
JS实现图片上传预览功能
2016/11/21 Javascript
JS实现分页浏览横向图片(类轮播)实例代码
2017/11/06 Javascript
AngularJS使用ui-route实现多层嵌套路由的示例
2018/01/10 Javascript
Vue实现点击后文字变色切换方法
2018/02/11 Javascript
layui框架table 数据表格的方法级渲染详解
2018/08/19 Javascript
vue生命周期和react生命周期对比【推荐】
2018/09/19 Javascript
vue中解决微信html5原生ios虚拟键返回不刷新问题
2020/10/20 Javascript
vuex页面刷新导致数据丢失的解决方案
2020/12/10 Vue.js
[01:14:41]DOTA2-DPC中国联赛定级赛 iG vs Magma BO3第一场 1月8日
2021/03/11 DOTA
基于Linux系统中python matplotlib画图的中文显示问题的解决方法
2017/06/15 Python
python负载均衡的简单实现方法
2018/02/04 Python
python 常见字符串与函数的用法详解
2018/11/23 Python
python用post访问restful服务接口的方法
2018/12/07 Python
Python实现微信消息防撤回功能的实例代码
2019/04/29 Python
PyCharm配置anaconda环境的步骤详解
2020/07/31 Python
Python Matplotlib绘图基础知识代码解析
2020/08/31 Python
使用html5制作loading图的示例
2014/04/14 HTML / CSS
美国最大的万圣节服装网站:HalloweenCostumes.com
2017/10/12 全球购物
如何将无状态会话Bean发布为WEB服务,只有无状态会话Bean可以发布为WEB服务?
2015/12/03 面试题
完美实现CSS垂直居中的11种方法
2021/03/27 HTML / CSS
行政专员岗位职责说明书
2014/09/01 职场文书
出售房屋委托书范本
2014/09/24 职场文书
2014年服装销售工作总结
2014/11/27 职场文书
一行代码python实现文件共享服务器
2021/04/22 Python
Python使用plt.boxplot()函数绘制箱图、常用方法以及含义详解
2022/08/14 Python