Task 2 索引、切片和迭代

it2025-12-16  10

Task 2 索引、切片和迭代

文章目录

Task 2 索引、切片和迭代副本和视图索引与切片1. 整数索引2. 切片索引3. dots索引4. 整数数组索引5. 布尔索引 数组迭代练习

副本和视图

在 Numpy 中,尤其是在做数组运算或数组操作时,返回结果不是数组的 副本 就是 视图。

所有赋值运算不会为数组和数组中的任何元素创建副本。

numpy.ndarray.copy() 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。 import numpy as np x=np.array([1,2,3,4,5]) y=x # 视图 print(y) # [1 2 3 4 5] print(id(x),id(y))# 2621145915760 2621145915760 x[1]=-1 # x array([ 1, -1, 3, 4, 5]) # y array([ 1, -1, 3, 4, 5]) x=np.array([1,2,3,4,5]) y=x.copy()#副本 print(id(x),id(y))#2621145382144 2621145382384

索引与切片

1. 整数索引

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

2. 切片索引

抽取数组的一部分元素生成新数组。

对 python 列表进行切片操作得到的数组是原数组的副本,而对 Numpy 数据进行切片操作得到的数组则是指向相同缓冲区的视图。

切片语法start:stop:step

不明确指明起始和结束位置的情况:

省去第一个数字,默认第一个数字是0;

省去第二个数字,默认第二个数字是数组的最大索引值;

省去最后一个数字,默认step=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[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]]

3. dots索引

使用...表示足够多的冒号来构建完整的索引列表

import numpy as np x = np.random.randint(1, 100, [2, 2, 3]) print(x) print(x) # [[[ 5 64 75] # [57 27 31]] # # [[68 85 3] # [93 26 25]]] print(x[1, ...]) # 即x[1,:,:] # [[68 85 3] # [93 26 25]] print(x[..., 2]) # [[75 31] # [ 3 25]]

4. 整数数组索引

方括号内传入多个索引值,可以同时选择多个元素。

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]] numpy. take(a, indices, axis=None, out=None, mode='raise') Take elements from an array along an axis.

整数数组索引,不是其子数组,是形成新的数组。

5. 布尔索引

通过一个布尔数组来索引目标数组。

import numpy as np 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.]

数组迭代

apply_along_axis(func1d, axis, arr) Apply a function to 1-D slices along the given axis. import numpy as np 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]

练习

交换数组arr中的列1和列2。 # 整数数组索引 import numpy as np arr = np.arange(9).reshape(3, 3) #[[0 1 2] # [3 4 5] # [6 7 8]] x=arr[:,[0,2,1]] #array([[0, 2, 1], #[3, 5, 4], #[6, 8, 7]]) 交换数组arr中的第1行和第2行。 # 整数数组索引 import numpy as np arr = np.arange(9).reshape(3, 3) #[[0 1 2] # [3 4 5] # [6 7 8]] x=arr[[0,2,1],:] #array([[0, 1, 2], #[6, 7, 8], #[3, 4, 5]]) 反转arr中的行 import numpy as np arr = np.arange(9).reshape(3, 3) print(arr) # [[0 1 2] # [3 4 5] # [6 7 8]] x = arr[::-1, :] print(x) # [[6 7 8] # [3 4 5] # [0 1 2]] 反转arr中的列 import numpy as np arr = np.arange(9).reshape(3, 3) print(arr) # [[0 1 2] # [3 4 5] # [6 7 8]] x = arr[:, ::-1] print(x) # [[2 1 0] # [5 4 3] # [8 7 6]]
最新回复(0)