MNIST数据集相关介绍

1. MNIST数据集加载与查看

1.1 内置数据集介绍

dir(tf.keras.datasets)

1.2 MNIST数据集加载

mnist = tf.keras.datasets.mnist
mnist.load_data()

1.3 数据集查看

x_train.shape, 
y_train.shape

import tensorflow as tf
#MNIST数据集查看
mnist=tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)


2. MNIST数据集可视化

2.1 导入依赖库

import matplotlib.pyplot as plt

2.2 随机选一个图片并查看lable

image_index = 123
print(y_train[image_index])

2.3 图片显示 

plt.imshow(x_train[image_index], cmap = 'Greys')

import tensorflow as tf
#MNIST数据集查看
mnist=tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

import matplotlib.pyplot as plt
#MNIST数据集可视化
image_index = 1234 #[0,59999)
plt.imshow(x_train[image_index])
plt.show()
print(y_train[image_index])

结果如下图 

图片位置在于:C:\Users\HP\.keras\datasets 


3. MNIST数据集格式转换


import tensorflow as tf
#MNIST数据集查看
mnist=tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

import matplotlib.pyplot as plt
#MNIST数据集可视化
image_index = 1234 #[0,59999)
#plt.imshow(x_train[image_index])
#plt.show()
print(y_train[image_index])

import numpy as np
#MNIST数据集格式转换
x_train = np.pad(x_train, ((0,0), (2,2), (2,2)), 'constant', constant_values=0)#将图片从28*28扩充为32*32
print(x_train.shape)

x_train = x_train.astype('float32')#数据类型转换
x_train /= 255 #数据正则化
x_train = x_train.reshape(x_train.shape[0], 32, 32, 1) #数据维度转换

print(x_train.shape)


发布了157 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/nanke_4869/article/details/104408500
今日推荐