開始使用免費開始

使用 NumPy 匯入平面檔案

在這個練習中,你將使用 numpy 的 loadtxt() 函式載入 MNIST 手寫數字辨識資料集,看看其實有多簡單:

  • 第一個參數是檔名。
  • 第二個參數是分隔符號,在本題中為逗號。

MNIST 資料集收錄了 0 到 9 的手寫數字,常用於機器學習領域。它用來作為評估演算法在識別與分類這些數字時效能的基準。

本練習屬於課程

Python 資料匯入入門

檢視課程

練習說明

  • file 與逗號 ','(作為分隔符號)傳入 np.loadtxt() 的參數。
  • print() 中填入參數以列印物件 digits 的型別。請使用 type() 函式。
  • 執行其餘程式碼,以視覺化資料中的其中一列。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import packages
import numpy as np

# Assign filename to variable: file
file = 'digits.csv'

# Load file as array: digits
digits = np.loadtxt(____, delimiter='____')

# Print datatype of digits
print(____)

# Select and reshape a row
im = digits[21, 1:]
im_sq = np.reshape(im, (28, 28))

# Plot reshaped data (matplotlib.pyplot already loaded as plt)
plt.imshow(im_sq, cmap='Greys', interpolation='nearest')
plt.show()
編輯並執行程式碼