benym的知识笔记 benym的知识笔记
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)

benym

惟其艰难,才更显勇毅🍂惟其笃行,才弥足珍贵
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)
  • Python-基础

    • assert语句的运用
    • list(列表)、tuple(元组)、dict(字典)的回顾
    • Python中的Docstring
    • Python中的多态
    • Python中的集合
    • Python中的列表
    • Python中的序列以及切片的解释
    • Python中的引用和切片
    • Python中的元组
    • Python中对列表和元组的切片操作
    • Python中完整for循环的实际运用
    • Python中字典(key-value)
    • Python中字符串的一些方法回顾(拆分与合并)
    • Python中字符串的一些方法回顾(切片回顾)
    • Python中字符串的一些方法回顾(文本对齐、去除空白)
    • Python中字符串的一些方法回顾
    • Python中字符串的一些基本操作
    • 多种方法快速交换两个变量的值
    • 利用Python进行文件的自动备份
    • 利用Python进行文件的自动备份(第二版)
    • 利用Python进行文件的自动备份(第三版和第四版)
    • 列表推导
    • 在函数中接受元组与字典
    • 装饰器
    • finally异常处理
    • Python的__name__ = '__main__' 的作用
    • Python的pickle模块
    • Python对象的实例化
    • Python日志模块
    • Python中的__new__方法的重写
    • Python中的lambda函数
    • Python中的静态方法、实例方法、类方法的区别
    • Python中的正则表达式
    • Python中的正则表达式match和search
    • Python中面向对象比较简单的内部函数
    • with open异常处理
    • 单例设计模式
    • 继承的运用
    • 简单的异常处理
    • 类变量与对象变量
    • 输入输出——简单的回文判断
    • 输入输出——回文字串的判断(加强版)
    • 文件操作
    • 用户自己引发的异常处理
    • 正则表达式检索与替换
    • 正则表达式中的compile函数
    • 正则表达式中的compile函数(二)
  • Python-机器学习

    • Numpy库的首次使用
    • kNN(k-近邻算法)
    • kNN识别手写图像
    • LogisticRegression(逻辑回归)
    • Ndarray对象
    • Numpy中的数组维度
    • Numpy中花式索引和shape用法
    • turtle绘图库
    • 第一个使用Tensorflow的程序
    • 将下载下来的MNIST手写数字数据集转化成为图片
      • 对于训练集的代码
      • 对于测试集的代码
    • Tensorflow交互式使用
    • 使用k-近邻算法改进约会网站的配对效果
    • Numpy数据类型和arange方法、astype方法的使用
    • 一些TensorFlow的基本操作
  • Python
  • Python-机器学习
benym
2018-08-26
目录

将下载下来的MNIST手写数字数据集转化成为图片

解析源文件下载(总共包含60000个训练数据和10000个测试数据)

  1. 训练集解析 (opens new window)
  2. 测试集解析 (opens new window)

# 对于训练集的代码

import numpy as np
import struct

from PIL import Image
import os

data_file = 'MNIST_data\\train-images.idx3-ubyte'  # 需要修改的路径
# It's 47040016B, but we should set to 47040000B
data_file_size = 47040016
data_file_size = str(data_file_size - 16) + 'B'

data_buf = open(data_file, 'rb').read()

magic, numImages, numRows, numColumns = struct.unpack_from(
    '>IIII', data_buf, 0)
datas = struct.unpack_from(
    '>' + data_file_size, data_buf, struct.calcsize('>IIII'))
datas = np.array(datas).astype(np.uint8).reshape(
    numImages, 1, numRows, numColumns)

label_file = 'MNIST_data\\train-labels.idx1-ubyte'  # 需要修改的路径

# It's 60008B, but we should set to 60000B
label_file_size = 60008
label_file_size = str(label_file_size - 8) + 'B'

label_buf = open(label_file, 'rb').read()

magic, numLabels = struct.unpack_from('>II', label_buf, 0)
labels = struct.unpack_from(
    '>' + label_file_size, label_buf, struct.calcsize('>II'))
labels = np.array(labels).astype(np.int64)

datas_root = 'MNIST_data\\'  # 需要修改的路径
if not os.path.exists(datas_root):
    os.mkdir(datas_root)

for i in range(10):
    file_name = datas_root + os.sep + str(i)
    if not os.path.exists(file_name):
        os.mkdir(file_name)

for ii in range(numLabels):
    img = Image.fromarray(datas[ii, 0, 0:28, 0:28])
    label = labels[ii]
    file_name = datas_root + os.sep + str(label) + os.sep + \
                'mnist_train_' + str(ii) + '.png'
    img.save(file_name)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

# 对于测试集的代码

import numpy as np
import struct

from PIL import Image
import os

data_file = 'MNIST_data\\t10k-images.idx3-ubyte'  # 需要修改的路径

# It's 7840016B, but we should set to 7840000B
data_file_size = 7840016
data_file_size = str(data_file_size - 16) + 'B'

data_buf = open(data_file, 'rb').read()

magic, numImages, numRows, numColumns = struct.unpack_from(
    '>IIII', data_buf, 0)
datas = struct.unpack_from(
    '>' + data_file_size, data_buf, struct.calcsize('>IIII'))
datas = np.array(datas).astype(np.uint8).reshape(
    numImages, 1, numRows, numColumns)

label_file = 'MNIST_data\\t10k-labels.idx1-ubyte'  # 需要修改的路径

# It's 10008B, but we should set to 10000B
label_file_size = 10008
label_file_size = str(label_file_size - 8) + 'B'

label_buf = open(label_file, 'rb').read()

magic, numLabels = struct.unpack_from('>II', label_buf, 0)
labels = struct.unpack_from(
    '>' + label_file_size, label_buf, struct.calcsize('>II'))
labels = np.array(labels).astype(np.int64)

datas_root = 'MNIST_data\\test_dataset'  # 需要修改的路径

if not os.path.exists(datas_root):
    os.mkdir(datas_root)

for i in range(10):
    file_name = datas_root + os.sep + str(i)
    if not os.path.exists(file_name):
        os.mkdir(file_name)

for ii in range(numLabels):
    img = Image.fromarray(datas[ii, 0, 0:28, 0:28])
    label = labels[ii]
    file_name = datas_root + os.sep + str(label) + os.sep + \
                'mnist_test_' + str(ii) + '.png'
    img.save(file_name)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
编辑 (opens new window)
#Python#机器学习
上次更新: 2022/12/31, 16:52:27
第一个使用Tensorflow的程序
Tensorflow交互式使用

← 第一个使用Tensorflow的程序 Tensorflow交互式使用→

最近更新
01
SpringCache基本配置类
05-16
02
DSTransactional与Transactional事务混用死锁场景分析
03-04
03
Rpamis-security-原理解析
12-13
更多文章>
Theme by Vdoing | Copyright © 2018-2024 benym | MIT License
 |   |   | 
渝ICP备18012574号 | 渝公网安备50010902502537号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式