国产探花免费观看_亚洲丰满少妇自慰呻吟_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()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武鸣县| 郁南县| 南通市| 育儿| 信宜市| 廊坊市| 和政县| 三穗县| 龙井市| 六盘水市| 孝义市| 敦化市| 北川| 扶沟县| 金坛市| 常熟市| 丹棱县| 田阳县| 柳江县| 绥江县| 岑溪市| 承德县| 安远县| 遂昌县| 徐闻县| 会宁县| 启东市| 饶阳县| 铁岭县| 都江堰市| 武功县| 运城市| 鄂尔多斯市| 平乐县| 大城县| 沾化县| 永兴县| 治县。| 日土县| 靖边县| 靖边县|