金狮镖局 Design By www.egabc.com
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手写识别
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无python MNIST手写识别数据调用API的方法的评论...
更新日志
2024年11月15日
2024年11月15日
- 茵达INDAHKUS《茵 the Blue》[FLAC/分轨][147.57MB]
- 老虎鱼AnneClarkwithUllavanDaelenJustinCiuche-Borderland-HiRes-Files24bit88.2kHz
- 江玲2004-百合盛开2CD[歌林][WAV+CUE]
- 孙露《怕什么孤单3CD》深圳音像[WAV分轨]
- s14全球总决赛目前是什么结果 s14全球总决赛赛程结果图一览
- s2TPA夺冠五人名单都有谁 tpa战队s2夺冠队员名单一览
- s13wbg战队成员都有谁 wbg战队s13成员名单一览
- 网友热议《幻兽帕鲁》停售、赔千万日元:那也赚麻了 不亏
- 这谁顶得住 《最终幻想7:重生》尤菲3D作品穿上终极芙蕾娜衣服
- 外媒称PS5pro违背承诺:《蜘蛛侠2》根本没法4K60帧
- 群星.2007-中文十大金曲30周年纪念专辑【RTHK】【WAV+CUE】
- 杨林.1989-留一点爱来爱自己【综一唱片】【WAV+CUE】
- 南合文斗.2007-陪君醉笑三千尘鸟人唱片】【FLAC+CUE】
- 群星《我们的歌第六季 第1期》[320K/MP3][90.72MB]
- 群星《我们的歌第六季 第1期》[FLAC/分轨][456.01MB]