numpy索引、切片与迭代

it2026-01-27  5

numpy索引、切片与迭代

索引与切片

整数索引 获取数组的单个元素,指定元素的索引即可

x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(x[2]) # 3 x = np.array([[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]]) print(x[2]) # [21 22 23 24 25] print(x[2][1]) # 22 print(x[2, 1]) # 22

切片索引

切片操作是指抽取数组的一部分元素生成新数组。对 python 列表进行切片操作得到的数组是原数组的副本,而对 Numpy 数据进行切片操作得到的数组则是指向相同缓冲区的视图。

如果想抽取(或查看)数组的一部分,必须使用切片语法,也就是,把几个用冒号( start:stop:step )隔开的数字置于方括号内。

为了更好地理解切片语法,还应该了解不明确指明起始和结束位置的情况。如省去第一个数字,numpy 会认为第一个数字是0;如省去第二个数字,numpy 则会认为第二个数字是数组的最大索引值;如省去最后一个数字,它将会被理解为1,也就是抽取所有元素而不再考虑间隔。

一维数组示例:

x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(x[0:2]) # [1 2] #用下标0~5,以2为步长选取数组 print(x[1:5:2]) # [2 4] print(x[2:]) # [3 4 5 6 7 8] print(x[:2]) # [1 2] print(x[-2:]) # [7 8] print(x[:-2]) # [1 2 3 4 5 6] print(x[:]) # [1 2 3 4 5 6 7 8] #利用负数下标翻转数组 print(x[::-1]) # [8 7 6 5 4 3 2 1]

二维数组示例:

x = np.array([[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]]) print(x[1:5:2]) # [[16 17 18 19 20] # [26 27 28 29 30]] print(x[:]) # [[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]] print(x[2, :]) # [21 22 23 24 25] print(x[:, 2]) # [13 18 23 28 33] print(x[0, 1:4]) # [12 13 14] print(x[1:4, 0]) # [16 21 26] print(x[1:3, 2:4]) # [[18 19] # [23 24]] print(x[:, :]) # [[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]] print(x[::2, ::2]) # [[11 13 15] # [21 23 25] # [31 33 35]] print(x[::-1, :]) # [[31 32 33 34 35] # [26 27 28 29 30] # [21 22 23 24 25] # [16 17 18 19 20] # [11 12 13 14 15]]

通过切片设置张量元素示例:

x[0::2, 1::3] = 0 print(x) # [[11 0 13 14 0] # [16 17 18 19 20] # [21 0 23 24 0] # [26 27 28 29 30] # [31 0 33 34 0]]

dos索引 NumPy 允许使用…表示足够多的冒号来构建完整的索引列表。例如:x为5维张量

x[1,2,…] 等于 x[1,2,:,:,:] x[…,3] 等于 x[:,:,:,:,3] x[4,…,5,:] 等于 x[4,:,:,5,:]

整数数组索引

传入索引值数组,可以同时选择多个元素 示例: x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) r = [0, 1, 2] print(x[r]) # [1 2 3] x = np.array([[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]]) r = [0, 1, 2] print(x[r]) # [[11 12 13 14 15] # [16 17 18 19 20] # [21 22 23 24 25]] r = [0, 1, 2] c = [2, 3, 4] y = x[r, c] print(y) # [13 19 25] 切片索引与数组索引组合: x = np.array([[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]]) y = x[0:3, [1, 2, 2]] print(y) # [[12 13 13] # [17 18 18] # [22 23 23]]

通过函数来进行调用(指定维度): numpy. take(a, indices, axis=None, out=None, mode=‘raise’) Take elements from an array along an axis. 示例:

x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) r = [0, 1, 2] print(np.take(x, r)) # [1 2 3] x = np.array([[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]]) r = [0, 1, 2] print(np.take(x, r, axis=0)) # [[11 12 13 14 15] # [16 17 18 19 20] # [21 22 23 24 25]] r = [0, 1, -1] print(np.take(x, r, axis=0)) # [[11 12 13 14 15] # [16 17 18 19 20] # [31 32 33 34 35]] r = [0, 1, 2] c = [2, 3, 4] y = np.take(x, [r, c]) print(y) # [[11 12 13] # [13 14 15]]

使用切片索引到numpy数组时,生成的数组视图将始终是原始数组的子数组, 但是整数数组索引,不是其子数组,是形成新的数组

a=np.array([[1,2],[3,4],[5,6]]) b=a[0:1,0:1] b[0,0]=2 print(a[0,0]==b) #[[True]] a=np.array([[1,2],[3,4],[5,6]]) b=a[0,0] b=2 print(a[0,0]==b) #False

布尔索引

x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = x > 5 print(y) # [False False False False False True True True] print(x[x > 5]) # [6 7 8] x = np.array([np.nan, 1, 2, np.nan, 3, 4, 5]) y = np.logical_not(np.isnan(x)) print(x[y]) # [1. 2. 3. 4. 5.]

数组迭代 除了for循环,Numpy 还提供另外一种更为优雅的遍历方法 apply_along_axis(func1d, axis, arr) Apply a function to 1-D slices along the given axis

x = np.array([[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]]) y = np.apply_along_axis(np.sum, 0, x) print(y) # [105 110 115 120 125] y = np.apply_along_axis(np.sum, 1, x) print(y) # [ 65 90 115 140 165] y = np.apply_along_axis(np.mean, 0, x) print(y) # [21. 22. 23. 24. 25.] y = np.apply_along_axis(np.mean, 1, x) print(y) # [13. 18. 23. 28. 33.] def my_func(x): return (x[0] + x[-1]) * 0.5 y = np.apply_along_axis(my_func, 0, x) print(y) # [21. 22. 23. 24. 25.] y = np.apply_along_axis(my_func, 1, x) print(y) # [13. 18. 23. 28. 33.]
最新回复(0)