数组形状变换
# 数组形状变换形式
- 重塑
- 扁平化处理
- 数组合并
- 数组拆分
- 数组扩充
- 数组转置和轴对换
# 重塑
- ndarray.reshape(shape, order='C')
- ndarray.resize()
reshape 函数返回修改后的新对象,而 ndarray.resize 方法修改数组本身
- 重塑的各个维度上整数的乘积必须等于 arr.size
- 如果想让自动计算某个轴上的大小,可以传入-1
import numpy as np
arr = np.arange(12)
arr2 = arr.copy()
1
2
3
4
2
3
4
arr.reshape((4, 3)) # order默认为‘C’ ,按列读取。等效于reshape(4, 3)
1
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
arr.reshape((4, 3), order='F') # 按行读取
# 虽然做了这么多操作 但是arr本身是没有变化的
arr
1
2
3
4
2
3
4
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
# resize()会修改对象本身
arr2.resize((4,3))
arr2
1
2
3
2
3
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
多维数组重塑
ndarr = arr.reshape(4, 3)
ndarr
1
2
2
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
ndarr.reshape(2, 6)
1
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
# 重塑为三维度
arr.reshape((2,2,3))
1
2
2
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
arr.reshape((3, 2, -1))
1
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])
# 扁平化处理
ndarr = np.arange(12).reshape(3, 4)
ndarr
1
2
2
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
ndarr.flatten() # order='C',按列
1
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
ndarr.flatten(order='F') # 按行
1
array([ 0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11])
ravel_ndarr = ndarr.ravel() # flatten()返回新对象,ravel()返回视图
ravel_ndarr
1
2
2
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
ravel_ndarr[1] = 100
ndarr
1
2
2
array([[ 0, 100, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
# 数组合并
concatenate
沿着一条轴连接一组(多个)数组。除了与 axis 对应的轴之外,其它轴必须有相同的形状。
vstack、row_stack
以追加行的方式对数组进行连接(沿轴 0)
hstack
以追加列的方式对数组进行连接(沿轴 1)
column_stack
类似于 hstack,但是会先将一维数组转换为二维列向量
dstack
以面向“深度”的方式对数组进行叠加
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
a
b
1
2
3
4
2
3
4
array([[1, 2],
[3, 4]])
array([[5, 6]])
np.concatenate((a, b), axis=0) # 可以查看函数说明
1
array([[1, 2],
[3, 4],
[5, 6]])
b.T
1
array([[5],
[6]])
a.shape, b.T.shape
1
((2, 2), (2, 1))
np.concatenate((a, a.T), axis=1)
1
array([[1, 2, 1, 3],
[3, 4, 2, 4]])
a = np.array([[1, 2, 3],
[4, 5, 6]])
b = np.array([[10, 20, 30],
[40, 50, 60]])
a
b
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
array([[1, 2, 3],
[4, 5, 6]])
array([[10, 20, 30],
[40, 50, 60]])
np.vstack((a, b))
1
array([[ 1, 2, 3],
[ 4, 5, 6],
[10, 20, 30],
[40, 50, 60]])
np.row_stack((a, b))
1
array([[ 1, 2, 3],
[ 4, 5, 6],
[10, 20, 30],
[40, 50, 60]])
np.hstack((a, b))
1
array([[ 1, 2, 3, 10, 20, 30],
[ 4, 5, 6, 40, 50, 60]])
np.column_stack((a, b))
1
array([[ 1, 2, 3, 10, 20, 30],
[ 4, 5, 6, 40, 50, 60]])
np.dstack((a, b))
1
array([[[ 1, 10],
[ 2, 20],
[ 3, 30]],
[[ 4, 40],
[ 5, 50],
[ 6, 60]]])
# 数组拆分
split
沿指定轴在指定的位置拆分数组
hsplit、vsplit、dsplit
split 的便捷化函数,分别沿轴 0、轴 1、轴 2 进行拆分
split_arr = np.arange(36).reshape(6, -1)
split_arr
1
2
2
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
a, b, c = np.split(split_arr, 3, axis=0) # 可以查看函数说明
a
b
c
1
2
3
4
2
3
4
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
array([[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
array([[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
a, b, c = np.split(split_arr, 3, axis=1) # 可以查看函数说明
a
b
c
1
2
3
4
2
3
4
array([[ 0, 1],
[ 6, 7],
[12, 13],
[18, 19],
[24, 25],
[30, 31]])
array([[ 2, 3],
[ 8, 9],
[14, 15],
[20, 21],
[26, 27],
[32, 33]])
array([[ 4, 5],
[10, 11],
[16, 17],
[22, 23],
[28, 29],
[34, 35]])
a, b, c, d = np.split(split_arr, [2, 4, 5], axis=1)
a
b
c
d
1
2
3
4
5
2
3
4
5
array([[ 0, 1],
[ 6, 7],
[12, 13],
[18, 19],
[24, 25],
[30, 31]])
array([[ 2, 3],
[ 8, 9],
[14, 15],
[20, 21],
[26, 27],
[32, 33]])
array([[ 4],
[10],
[16],
[22],
[28],
[34]])
array([[ 5],
[11],
[17],
[23],
[29],
[35]])
# repeat 和 tile
# repeat —— 针对元素
arr = np.arange(4)
arr
1
2
2
array([0, 1, 2, 3])
np.repeat(arr, 2)
1
array([0, 0, 1, 1, 2, 2, 3, 3])
# 指定每个元素的重复次数
# 0重复2次,1重复3次,2重复4次,3重复5次
np.repeat(arr, [2, 3, 4, 5])
1
2
3
2
3
array([0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3])
多维数组 repeat
ndarr = np.arange(6).reshape(2, 3)
ndarr
1
2
2
array([[0, 1, 2],
[3, 4, 5]])
np.repeat(ndarr, 2) # 不指定轴会被扁平化
1
array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
np.repeat(ndarr, 2, axis=0)
1
array([[0, 1, 2],
[0, 1, 2],
[3, 4, 5],
[3, 4, 5]])
np.repeat(ndarr, 2, axis=1)
1
array([[0, 0, 1, 1, 2, 2],
[3, 3, 4, 4, 5, 5]])
# tile ———— 针对整个数组
ndarr
1
array([[0, 1, 2],
[3, 4, 5]])
np.tile(ndarr, 2) # 对标量是横向扩展
1
array([[0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5]])
np.tile(ndarr, (1,2))
1
array([[0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5]])
np.tile(ndarr, (2,3))
1
array([[0, 1, 2, 0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5, 3, 4, 5],
[0, 1, 2, 0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5, 3, 4, 5]])
# 数组转置和轴对换
- 转置和轴对换返回的是原对象的视图,不是新对象
arr = np.arange(12).reshape(3,4)
arr
1
2
2
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
arr.T
1
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
arr.transpose()
1
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
arr
1
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
arr.T[:2] = 0
arr
1
2
2
array([[ 0, 0, 2, 3],
[ 0, 0, 6, 7],
[ 0, 0, 10, 11]])
上次更新: 2023/11/01, 03:11:44