国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python MNIST手寫識(shí)別數(shù)據(jù)調(diào)用API的方法

2020-01-04 14:55:00
字體:
供稿:網(wǎng)友

MNIST數(shù)據(jù)集比較小,一般入門機(jī)器學(xué)習(xí)都會(huì)采用這個(gè)數(shù)據(jù)集來訓(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 osimport structimport numpy as npimport matplotlib.pyplot as pltdef 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é)果如下:

python,MNIST,手寫識(shí)別

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沛县| 台南市| 历史| 旬阳县| 岳西县| 疏附县| 怀宁县| 句容市| 九台市| 河曲县| 冷水江市| 修文县| 乌兰察布市| 芜湖市| 榕江县| 长治县| 澜沧| 聂拉木县| 长白| 津市市| 临清市| 南丰县| 城步| 大同县| 宝鸡市| 桂阳县| 启东市| 长汀县| 尼玛县| 华蓥市| 永兴县| 克东县| 改则县| 嘉黎县| 苏州市| 宁津县| 饶平县| 汉中市| 汉中市| 小金县| 遂溪县|