整数索引
一般形式:arr[frist_dim_index,second_dim_index,...,nth_dim_index]
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x[2]) # 3
y = 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]
# x[2] == x[2,:]
print(x[2, 1]) # 22
# x[2, 1] == x[2][1]
切片索引
一般形式:arr[frist_dim_slice,second_dim_slice,...,nth_dim_slice]
Dots 索引
NumPy 允许使用 ...
表示足够多的冒号来构建完整的索引列表。
x[1,2,...]
等于x[1,2,:,:,:]
x[...,3]
等于x[:,:,:,:,3]
x[4,...,5,:]
等于x[4,:,:,5,:]
整数数组索引
一般形式:arr[frist_dim,second_dim,...,nth_dim]
一维数组,一维索引:
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]]
# x[r] 代表除了第零维,其他维度省略,等同于:[(0,:),(1,:),(2,:)]
r = [0, 1, 2]
c = [2, 3, 4]
y = x[r, c]
print(y)
# [13 19 25]
# x[r,c] 所有维度都没有省略,代表找出[(0,2),(1,3),(2,4)]的元素。
一维数组,多维索引:
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
r = np.array([[0, 1], [3, 4]])
print(x[r])
# [[1 2]
# [4 5]]
# x[r],只对第零维进行操作,找出第零维的索引,(0,1)一组,(3,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 = np.array([[0, 1], [3, 4]])
print(x[r])
# [[[11 12 13 14 15]
# [16 17 18 19 20]]
#
# [[26 27 28 29 30]
# [31 32 33 34 35]]]
# x[r],只对第零维进行操作,找出第零维的索引,(0,1)一组,(3,4)一组,构成多维数组
# 相当于 [[(0,:),(1,:)][(3,:),(4,:)]]
r = np.array([[0, 0], [4, 4]])
c = np.array([[0, 4], [0, 4]])
y = x[r, c]
print(y)
# [[11 15]
# [31 35]]
# 获取[[(0,0),(0,4)][(4,0),(4,4)]] 的元素
r = np.array([0,0])
c = np.array([[0, 4], [0, 4]])
y = x[r, c]
print(y)
# [[11, 15],
# [11, 15]]
# 获取[[(0,0),(0,4)][(0,0),(0,4)]] 的元素
# 相当于 r = np.array([[0,0],[0,0]]) 广播机制
切片 + 索引
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]]
# 第零维:(0:3) 取走,第一维取出[1,2,2]元素
np.take()
从指定轴抽取元素
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]]
print(np.take(x, r, axis=1))
#[[11 12 13]
# [16 17 18]
# [21 22 23]
# [26 27 28]
# [31 32 33]]
切片索引和整数索引的区别
切片索引所生成的数组视图始终是原始数组的子数组;但是整数索引生成的数组不是其子数组,而是新的数组
# 切片索引
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
优雅的数组迭代
apply_along_axis(func1d, axis, arr)
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]
这个地方很容易弄反,二维数据拥有两个轴:第 0 轴沿着行的垂直往下,第 1 轴沿着列的方向水平延伸。
比如 axis=0 时,是对垂直方向的数据进行依次操作;axis=1 时,是对水平方向的数据进行依次操作。