python MNIST手寫識(shí)別數(shù)據(jù)調(diào)用API的方法
MNIST數(shù)據(jù)集比較小,一般入門機(jī)器學(xué)習(xí)都會(huì)采用這個(gè)數(shù)據(jù)集來(lái)訓(xùn)練
下載地址:yann.lecun.com/exdb/mnist/
有4個(gè)有用的文件:
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. 數(shù)據(jù)集存儲(chǔ)是用binary file存儲(chǔ)的,黑白圖片。
下面給出load數(shù)據(jù)集的代碼:
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()
跑出的結(jié)果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo)
今天小編就為大家分享一篇Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Django定制Admin頁(yè)面詳細(xì)實(shí)例(展示頁(yè)面和編輯頁(yè)面)
django自帶的admin因?yàn)楣δ芎蜆邮奖容^簡(jiǎn)陋,常常需要再次定制,下面這篇文章主要給大家介紹了關(guān)于Django定制Admin頁(yè)面(展示頁(yè)面和編輯頁(yè)面)的相關(guān)資料,需要的朋友可以參考下2023-06-06對(duì)python requests的content和text方法的區(qū)別詳解
今天小編就為大家分享一篇對(duì)python requests的content和text方法的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python爬蟲之爬取最新更新的小說(shuō)網(wǎng)站
這篇文章主要介紹了Python爬蟲之爬取最新更新的小說(shuō)網(wǎng)站,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python爬蟲的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05python用matplotlib繪制二維坐標(biāo)軸,設(shè)置箭頭指向,文本內(nèi)容方式
這篇文章主要介紹了python用matplotlib繪制二維坐標(biāo)軸,設(shè)置箭頭指向,文本內(nèi)容方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08