NumPy数组除了存储数据外,还附带了许多有用的属性,它们描述了数组的元信息。通过这些属性,你可以快速了解数组的结构、内存占用和数据类型等关键信息。
ndim:数组的维度数
ndim 返回数组的维度数量(即轴的数量)。一维数组的 ndim = 1,二维数组为2,以此类推。
import numpy as np
# 一维数组
a = np.array([1, 2, 3, 4])
print("一维数组 ndim =", a.ndim)
# 二维数组
b = np.array([[1, 2, 3], [4, 5, 6]])
print("二维数组 ndim =", b.ndim)
# 三维数组
c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("三维数组 ndim =", c.ndim)
输出:
一维数组 ndim = 1
二维数组 ndim = 2
三维数组 ndim = 3
shape:数组的形状
shape 是一个元组,表示每个维度的大小。例如,(3, 4) 表示一个3行4列的二维数组。
# 一维数组
a = np.array([1, 2, 3, 4])
print("a.shape =", a.shape)
# 二维数组
b = np.array([[1, 2, 3], [4, 5, 6]])
print("b.shape =", b.shape)
# 三维数组
c = np.arange(24).reshape(2, 3, 4)
print("c.shape =", c.shape)
输出:
a.shape = (4,)
b.shape = (2, 3)
c.shape = (2, 3, 4)
(n,),这表示一个具有n个元素的一维数组,注意逗号的存在。
size:数组元素的总个数
size 返回数组中所有元素的个数,等于各维度大小的乘积。
a = np.array([[1, 2, 3], [4, 5, 6]])
print("a.size =", a.size) # 2*3 = 6
b = np.arange(12).reshape(3, 4)
print("b.size =", b.size) # 12
输出:
a.size = 6
b.size = 12
dtype:数组元素的数据类型
dtype 返回数组中元素的类型。NumPy支持多种数据类型,如 int32, float64, bool, complex 等。
int_arr = np.array([1, 2, 3])
print("int_arr.dtype =", int_arr.dtype)
float_arr = np.array([1.0, 2.0, 3.0])
print("float_arr.dtype =", float_arr.dtype)
bool_arr = np.array([True, False, True])
print("bool_arr.dtype =", bool_arr.dtype)
complex_arr = np.array([1+2j, 3+4j])
print("complex_arr.dtype =", complex_arr.dtype)
# 也可以显式指定类型
custom = np.array([1, 2, 3], dtype=np.float32)
print("custom.dtype =", custom.dtype)
输出:
int_arr.dtype = int64
float_arr.dtype = float64
bool_arr.dtype = bool
complex_arr.dtype = complex128
custom.dtype = float32
itemsize:每个元素的字节大小
itemsize 返回数组中每个元素所占用的字节数。例如,float64 的 itemsize 为8,int32 为4。
arr_int = np.array([1, 2, 3], dtype=np.int32)
print("int32 itemsize =", arr_int.itemsize, "字节")
arr_float = np.array([1.0, 2.0, 3.0], dtype=np.float64)
print("float64 itemsize =", arr_float.itemsize, "字节")
arr_bool = np.array([True, False], dtype=bool)
print("bool itemsize =", arr_bool.itemsize, "字节")
输出:
int32 itemsize = 4 字节
float64 itemsize = 8 字节
bool itemsize = 1 字节
nbytes:数组总字节数
nbytes 返回整个数组占用的内存字节数,等于 size * itemsize。
arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
print("arr.nbytes =", arr.nbytes, "字节")
print("验证: size * itemsize =", arr.size * arr.itemsize)
输出:
arr.nbytes = 48 字节
验证: size * itemsize = 48
T:转置视图
对于二维数组,T 属性返回数组的转置(行变列,列变行)。对于一维数组,T 返回自身(因为一维数组的转置仍然是自身)。注意,它返回的是原数组的视图,并非副本。
# 二维数组转置
mat = np.array([[1, 2, 3], [4, 5, 6]])
print("原数组:\n", mat)
print("转置后 (mat.T):\n", mat.T)
# 一维数组转置(不变)
vec = np.array([1, 2, 3, 4])
print("一维数组 T 不变:", vec.T)
输出:
原数组:
[[1 2 3]
[4 5 6]]
转置后 (mat.T):
[[1 4]
[2 5]
[3 6]]
一维数组 T 不变: [1 2 3 4]
flat:平坦迭代器
flat 返回一个迭代器,可以按行优先(C风格)顺序遍历数组的所有元素,如同将数组拉平为一维数组。
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("使用 flat 遍历:")
for element in arr.flat:
print(element, end=" ")
# 也可以通过索引访问扁平化后的元素
print("\narr.flat[3] =", arr.flat[3])
输出:
使用 flat 遍历:
1 2 3 4 5 6
arr.flat[3] = 4
real 和 imag:对于复数数组,返回实部和虚部。data:指向数组数据缓冲区的指针(通常不直接使用)。strides:每个维度在内存中前进一个元素所需的字节数(高级属性)。# 复数数组示例
cplx = np.array([1+2j, 3+4j, 5+6j])
print("实部:", cplx.real)
print("虚部:", cplx.imag)
# strides 示例
arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)
print("strides:", arr.strides) # 通常为 (12, 4),表示跨过一行需要12字节,跨过一列需要4字节
输出:
实部: [1. 3. 5.]
虚部: [2. 4. 6.]
strides: (12, 4)
本节介绍了NumPy数组的核心属性:ndim、shape、size、dtype、itemsize、nbytes、T 和 flat。通过掌握它们,你可以轻松获取数组的基本信息,并为后续的数组操作打下坚实基础。接下来,我们将学习如何通过索引和切片访问数组元素。