chapter
digits
Is a handwritten digital data set, we can use Python data visualization libraries such as matplotlib, to view these handwritten digital images.
Examples
Displaying digits.images
handwritten digital image.
from sklearn import datasets
# 加载 `digits` 数据集
digits = datasets.load_digits()
# 导入 matplotlib
import matplotlib.pyplot as plt
# 设置图形大小(宽、高)以英寸为单位
fig = plt.figure(figsize=(6, 6))
# 设置子图形布局,如间隔之类...
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
# 对于64幅图像中的每一幅
for i in range(64):
# 初始化子图:在8×8的网格中,在第i+1个位置添加一个子图
ax = fig.add_subplot(8, 8, i + 1, xticks=[], yticks=[])
# 在第i个位置显示图像
ax.imshow(digits.images[i], cmap=plt.cm.binary, interpolation='nearest')
# 用目标值标记图像
ax.text(0, 7, str(digits.target[i]))
# 显示图形
plt.show()
Export
We can also use digits.target
the marking target digits.images
sample image data format, and display.
Examples
Displayed digits.images
in the first eight handwritten digital images, and a mark image corresponding to the target value.
from sklearn import datasets
# 加载 `digits` 数据集
digits = datasets.load_digits()
# 导入 matplotlib
import matplotlib.pyplot as plt
# 把图像和目标标签组合成一个列表
images_and_labels = list(zip(digits.images, digits.target))
# 对于列表(前8项)中的每个元素
for index, (image, label) in enumerate(images_and_labels[:8]):
# 在第i+1个位置初始化一个2X4的子图
plt.subplot(2, 4, index + 1)
# 不要画坐标轴
plt.axis('off')
# 在所有子图中显示图像
plt.imshow(image, cmap=plt.cm.gray_r,interpolation='nearest')
# 为每个子图添加一个标题(目标标签)
plt.title('Training: ' + str(label))
# 显示图形
plt.show()
display: