章節
- SciKit-Learn 加載資料集
- SciKit-Learn 資料集基本資訊
- SciKit-Learn 使用matplotlib可視化資料
- SciKit-Learn 可視化資料:主成分分析(PCA)
- SciKit-Learn 預處理資料
- SciKit-Learn K均值聚類
- SciKit-Learn 支援向量機
- SciKit-Learn 速查
digits
是一個手寫數字的資料集,我們可以使用Python的資料可視化庫,比如matplotlib,來檢視這些手寫數字圖像。
示例
顯示
digits.images
中的手寫數字圖像。
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()
輸出
我們也可以使用
digits.target
中的目标值标記
digits.images
圖像格式的樣本資料,并顯示。
示例
顯示
digits.images
中的前8個手寫數字圖像,并用對應的目标值标記圖像。
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()
顯示: