array 大多数情况下都是以多维的形式出现的,一般对超过二维的多维 array 称为「张量」,二维矩阵,一维向量。因为多维度,所以自然而然地涉及到形状的改变和转换,可以算是张量最基础的「操作」了。
本节我们主要涉及以下三个方面:
其中,改变形状和转置都非常常用,我们建议您熟练掌握。
⭐⭐⭐⭐⭐
这小节里面的 API 使用非常高频,尤其是扩展 1 维度的 expand_dims
和去除 1 维度的 squeeze
,您未来会在很多神经网络架构中看到这俩货的身影。
⚠️ 需要注意的是:无论是扩展还是缩减,多或少的 shape 都是 1,squeeze
时如果指定维度,则该维度 shape 必须是 1。
# 换个整数的随机 array
rng = np.random.default_rng(seed=42)
arr = rng.integers(1, 100, (3, 4))
arr
array([[ 9, 77, 65, 44],
[43, 86, 9, 70],
[20, 10, 53, 97]])
# 有时候您可能需要将多维 array 打平
arr.ravel()
array([ 9, 77, 65, 44, 43, 86, 9, 70, 20, 10, 53, 97])
arr.shape
(3, 4)
np.expand_dims
#### 扩展 1 个维度,需要(必须)指定维度
# 其实就是多嵌套了一下
np.expand_dims(arr, 1).shape
(3, 1, 4)
# 扩充维度
expanded = np.expand_dims(arr, axis=(1, 3, 4))
expanded.shape
(3, 1, 4, 1, 1)
# 扩充维度不能跳跃
expanded = np.expand_dims(arr, axis=(1, 3, 8))
AxisError: axis 8 is out of bounds for array of dimension 5
np.squeeze
# squeeze 指定 axis 的shape必须为1
np.squeeze(expanded, axis=0)
ValueError: cannot select an axis to squeeze out which has size not equal to one
# 如果指定了维度,那就只会去除该维度,指定的维度必须为 1
np.squeeze(expanded, axis=1).shape
(3, 3, 1, 1)
# 去除所有维度为 1 的
np.squeeze(expanded).shape
(3, 3)
np.reshape/arr.reshape
# reshape 成另一个形状
# 也可以直接变为一维向量
arr.reshape(2, 2, 3)
array([[[ 9, 77, 65],
[44, 43, 86]],
[[ 9, 70, 20],
[10, 53, 97]]])
# 可以偷懒,使用 -1 表示其他维度(此处 -1 为 3),注意,reshape 参数可以是 tuple 或连续整数
arr1 = arr.reshape((4, -1))
arr1
array([[ 9, 77, 65],
[44, 43, 86],
[ 9, 70, 20],
[10, 53, 97]])
# 元素数量必须与原array一致
arr.reshape(3, 3)
ValueError: cannot reshape array of size 12 into shape (3,3)
# 另一种变换形状的方式 —— 原地变换
# 不过不能用-1
# 另外 resize 不一定和原来的元素数量一样多
arr2 = arr.resize((4, 3))
# 注意:上面的 reshape 会生成一个新的 array,但 resize 不会,所以我们需要用原变量名将它显示出来
# arr2 没有值
arr2
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 9, 70, 20],
[10, 53, 97]])
# 直接 resize,如果元素数量多时会提示错误
arr.resize((2, 3))
ValueError: cannot resize an array that references or is referenced
by another array in this way.
Use the np.resize function or refcheck=False
# 可以copy一份
arrcopy = np.copy(arr)
arrcopy.resize((2, 3))
arrcopy
array([[ 9, 77, 65],
[44, 43, 86]])
# arr 保持不变
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 9, 70, 20],
[10, 53, 97]])
# 也可以将 refcheck 设为 False
# 此时 arr 会发生变化
# 元素数量超出时,截断;元素数量不够时,0填充
arr.resize((2,3), refcheck=False)
arr
array([[ 9, 77, 65],
[44, 43, 86]])
arr.resize((3, 3), refcheck=False)
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 0, 0, 0]])
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 0, 0, 0]])
# 如果用 np.resize 会略有不同
# 元素数量不够时,会自动复制
np.resize(arr, (5, 3))
array([[ 9, 77, 65],
[44, 43, 86],
[ 0, 0, 0],
[ 9, 77, 65],
[44, 43, 86]])
# 元素数量多出来时,会自动截断
np.resize(arr, (2, 2))
array([[ 9, 77],
[65, 44]])
⭐
也可以看作是一种对原数组的转换,用的不多,可以了解一下,为接下来的索引和切片做个热身。
如果给一个字符串或数组让您反序,您可能会想到很多种方法,比如:reversed
,或者写一个方法,或者用 Python list 的索引功能,而这也是 numpy
中 array 反序的方式。
# 字符串
s = "uevol"
s[::-1]
'loveu'
# 数组
lst = [1, "1", 5.2]
lst[::-1]
[5.2, '1', 1]
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 9, 70, 20],
[10, 53, 97]])
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 9, 70, 20],
[10, 53, 97]])
# 我们按上面的套路:默认列反序
arr[::-1]
array([[10, 53, 97],
[ 9, 70, 20],
[44, 43, 86],
[ 9, 77, 65]])
# 列不变行反序
arr[::-1, :]
array([[10, 53, 97],
[ 9, 70, 20],
[44, 43, 86],
[ 9, 77, 65]])
# 在不同维度上操作:行不变列反序
arr[:, ::-1]
array([[65, 77, 9],
[86, 43, 44],
[20, 70, 9],
[97, 53, 10]])
# 行变列也变
arr[::-1, ::-1]
array([[97, 53, 10],
[20, 70, 9],
[86, 43, 44],
[65, 77, 9]])
⭐⭐⭐
转置是线性代数的基本操作,拿二维矩阵为例,通俗理解就是把它放倒,shape 反转,行变成列,列成为行。当然,对于多维也是类似的。我们建议您二维矩阵用 arr.T
(会快很多),超过二维的张量可以用 np.transpose
,会更加灵活些。
⚠️ 需要注意的是:一维数组转置还是自己。
# 一维
np.array([1,2]).T.shape
(2,1)
arr
array([[ 9, 77, 65],
[44, 43, 86],
[ 9, 70, 20],
[10, 53, 97]])
arr.T
# 简便用法,把所有维度顺序都给倒过来
arr.T
array([[ 9, 44, 9, 10],
[77, 43, 70, 53],
[65, 86, 20, 97]])
# 将 shape=(1,1,3,4) 的转置后得到 shape=(4,3,1,1)
arr.reshape(1, 1, 3, 4).T.shape
(4, 3, 1, 1)
# 同上
arr.reshape(1, 2, 2, 1, 3, 1).T.shape
(1, 3, 1, 2, 2, 1)
np.transpose
# 这种转置方式可以指定 axes
np.transpose(arr)
array([[ 9, 44, 9, 10],
[77, 43, 70, 53],
[65, 86, 20, 97]])
# 不指定 axes 时和 .T 是一样的
np.transpose(arr.reshape(1, 2, 2, 1, 3, 1)).shape
(1, 3, 1, 2, 2, 1)
# 指定 axes,不过 axes 数量必须包含所有维度的序列
# 比如两个维度就是 (0, 1),四个就是 (0, 1, 2, 3)
# 当然,顺序可以改变,比如下面就是只转置第 2 个和第 3 个维度
# 注意,只有超过 2 维时,这样才有意义
# 下面的结果中,中间2个维度被调换顺序了,顺序就在axes中指定的
np.transpose(arr.reshape(1, 1, 3, 4), axes=(0, 2, 1, 3)).shape
(1, 3, 1, 4)