开始使用免费开始使用

使用 NumPy 导入平面文件

在本练习中,您将使用 numpy 函数 loadtxt() 加载 MNIST 手写数字识别数据集,看看这会有多简单:

  • 第 1 个参数是文件名。
  • 第 2 个参数是分隔符,本例中为逗号。

MNIST 数据集是由 0 到 9 的手写数字构成,常用于机器学习领域。它常被用作评测算法在识别与分类这些数字时性能的基准数据集。

本练习是课程的一部分

Python 数据导入入门

查看课程

练习说明

  • np.loadtxt() 中传入参数:file 以及作为分隔符的逗号 ','
  • 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()
编辑并运行代码