python MNIST手写识别数据调用API的方法
作者:caichao08 发布时间:2021-05-13 19:20:45
标签:python,MNIST,手写识别
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()
跑出的结果如下:
来源:https://blog.csdn.net/caichao08/article/details/78988389


猜你喜欢
- 1.func Copy(dst Writer, src Reader) (written int64, err error)这个函数是从一个
- hypot()方法返回的欧几里德范数 sqrt(x*x + y*y).语法以下是hypot()方法的语法:hypot(x, y)
- 需求:如下图所示实现sql语句SELECT A1,SUM(A2*A3) FROM A GROUP BY A1大家可以自行测试一下,主要需要了
- 前言之前用过Eel做的桌面应用觉得已经够 * 了,不过由于Eel是调用Chrome,时常出现各种小问题,比如窗口大小设置后有时候不管用,鼠标右键
- 1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字
- 如: 0.625 取 1 2.1 取3 3.6 取4 <% if fix(a)>a then b=fix(a) else b=f
- 1. 生成源码# -*- coding: utf-8 -*-import randomdef generate_verification_c
- 基本简介dot函数为numpy库下的一个函数,主要用于矩阵的乘法运算,其中包括:向量内积、多维矩阵乘法和矩阵与向量的乘法。1. 向量内积向量
- TIMESTAMPDIFF函数用于计算两个日期的时间差语法结构TIMESTAMPDIFF(unit,datetime_expr1,datet
- 文章先介绍了关于俄罗斯方块游戏的几个术语。边框——由10*20个空格组成,方块就落在这里面。盒子——组成方块的其中小方块,是组成方块的基本单
- 下面给大家分享Python爬虫后获取重定向url的两种方法,具体内容如下所示;方法(一)# 获得重定向url from urllib imp
- 引用PyMongo>>> import pymongo创建连接Connection>>> import
- numpy 中 的random模块有多个函数用于生成不同类型的随机数,常见的有 uniform、rand、random、randint、ra
- 例子:Response.Cookies("letwego")("visiter")="84
- asp函数实现把数字格式化为每3个数字时以逗号间隔的数字见下:<%Function Comma(str)If No
- 启动targetcli时遭遇ImportError: cannot import name ALUATargetPortGrou
- 楔子pandas 支持我们从 Excel、CSV、数据库等不同数据源当中读取数据,来构建 DataFrame。但有时数据并不来自这些外部数据
- 以前提取这些文件用的是一同事些的批处理文件;用起来不怎么顺手,刚好最近在学些python,所有就自己动手写了一个python提取文件的小程序
- import webbrowser as webimport timeimport oscount=0while count<10:&
- 本文实例讲述了Python使用CMD模块更优雅的运行脚本的方法。分享给大家供大家参考。具体分析如下:平时由于经常给测试人员调试一些东西,虽然