0Pricing
Data Science Academy · 课时

形状、大小与 dtype

了解数组的结构

形状、大小与 dtype 是 CoddyKit 上的免费 Data Science Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Data Science Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Data Science Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Read the Array's Anatomy

Every array carries metadata about itself. Three labels tell you almost everything: its shape, its size, and its dtype. Let's read each one. 🔍

Shape: Rows and Columns

The shape is a tuple of dimension lengths. A 3-by-4 grid reports (3, 4): three rows down, four columns across.

a = np.zeros((3, 4))
a.shape  # (3, 4)

Shape Tells You the Layout

Shape is your first sanity check. If a model expects (100, 5) and you hand it (5, 100), the shape mismatch is where bugs hide.

ndim: How Many Dimensions

The ndim attribute counts dimensions. A flat row is 1, a table is 2, a stack of tables is 3. It is just the length of the shape.

a.ndim  # 2

Size: Total Element Count

The size is the total number of elements, the product of every shape value. A (3, 4) array has a size of 12.

a.size  # 12

dtype: The One Shared Type

The dtype names the single type all elements share, like int64 or float64. It controls both precision and memory use.

np.array([1, 2, 3]).dtype  # int64

Choose Your dtype on Purpose

You can set the type yourself with the dtype argument. Use a smaller type like int32 to save memory on huge arrays.

np.array([1, 2, 3], dtype='int32')

dtype Affects Precision

float32 holds fewer decimals than float64. Tiny rounding gaps add up, so pick the type your numbers actually need.

Convert With astype

Already have an array? astype returns a copy in a new type, handy for turning floats into clean integers.

f = np.array([1.0, 2.0])
f.astype('int64')

itemsize: Bytes Per Element

The itemsize tells you bytes per element. Multiply it by size to estimate the array's total memory footprint.

a.itemsize  # 8 for float64

Inspect Before You Compute

Checking shape and dtype first saves hours. They reveal mismatched data and wrong types before a calculation quietly goes wrong. ✅

Quick Check

Let's test your read of an array's shape.

Recap

You can now read an array at a glance: shape for layout, ndim and size for counts, and dtype for the shared type. These four checks catch most bugs early. 🎉

常见问题解答

「形状、大小与 dtype」课时是免费的吗?

是的 — 「形状、大小与 dtype」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Data Science Academy 课程的其余内容,请升级到 CoddyKit PRO。 Data Science Academy 课程共包含 4 节课。

「形状、大小与 dtype」这节课中我会学到什么?

了解数组的结构 你通过在浏览器中直接运行的动手代码来练习 Data Science Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Data Science Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Data Science Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「形状、大小与 dtype」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Data Science Academy 课中编写并运行代码吗?

能。每节 Data Science Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 从 Python 列表到 ndarray
  2. 形状、大小与 dtype
  3. 无需循环的向量化数学运算
  4. 数组索引与切片
← 返回 Data Science Academy