python MNIST手写识别数据调用API的方法

 更新时间:2018年08月08日 10:54:35   作者:caichao08  
这篇文章主要介绍了python MNIST手写识别数据调用API的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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 collections模块的使用

    python collections模块的使用

    这篇文章主要介绍了python collections模块的使用,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-10-10
  • Python爬取视频时长场景实践示例

    Python爬取视频时长场景实践示例

    这篇文章主要为大家介绍了Python获取视频时长场景实践示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 使用python脚本自动生成K8S-YAML的方法示例

    使用python脚本自动生成K8S-YAML的方法示例

    这篇文章主要介绍了使用python脚本自动生成K8S-YAML的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • pytorch + visdom CNN处理自建图片数据集的方法

    pytorch + visdom CNN处理自建图片数据集的方法

    这篇文章主要介绍了pytorch + visdom CNN处理自建图片数据集的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 解决BN和Dropout共同使用时会出现的问题

    解决BN和Dropout共同使用时会出现的问题

    这篇文章主要介绍了解决BN和Dropout共同使用时会出现的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Python键值互换的实现示例

    Python键值互换的实现示例

    Python键值互换是一种对Python字典类型中键值对进行反转的技术,有时候,我们需要以值作为键,以键作为值来操作字典,这时候就需要用到键值互换的技术,本文主要介绍了Python键值互换的实现示例,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • 深入详解Python中Micawber库的使用

    深入详解Python中Micawber库的使用

    Python Micawber库就是一个用于解析和嵌入媒体资源的工具,它可以自动识别各种媒体资源的URL,下面就跟随小编一起来看看它的具体使用吧
    2022-06-06
  • Python: glob匹配文件的操作

    Python: glob匹配文件的操作

    这篇文章主要介绍了Python: glob匹配文件的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • python Django模板的使用方法

    python Django模板的使用方法

    这篇文章主要为大家介绍了python Django模板的使用方法,代码很详细,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • 一文带你了解Python与svg之间的操作

    一文带你了解Python与svg之间的操作

    svgwrite是一个 Python 库,用于生成简单的 SVG 图片。它提供了一组类似于绘图的 API,使用者可以在 SVG 画布上画线、矩形、圆等图形。本文主要介绍了如何利用svgwrite进行SVG图片的操作,需要的可以参考一下
    2023-01-01

最新评论