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

首頁 > 編程 > Python > 正文

python MNIST手寫識別數據調用API的方法

2020-02-15 22:43:17
字體:
來源:轉載
供稿:網友

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 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()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌苏市| 达日县| 广河县| 通州区| 嘉义市| 会泽县| 昆山市| 田东县| 天祝| 庄河市| 平果县| 措勤县| 玛多县| 昌平区| 元氏县| 西乌珠穆沁旗| 禹州市| 平昌县| 云龙县| 白河县| 枣强县| 宝坻区| 卓尼县| 新泰市| 额济纳旗| 上虞市| 奇台县| 民乐县| 会昌县| 泾源县| 淮北市| 夏津县| 宜川县| 泰兴市| 射洪县| 广宁县| 兴安盟| 柯坪县| 巨野县| 崇礼县| 镇沅|