NumPy数组访问
NumPy数组访问
- 索引访问
- 切片访问
- 布尔索引
- 花式索引
- 迭代数组
- 1、默认遍历
- 2、控制顺序
- 行优先
- 列优先
- 3、读写模式
- 4、其他参数
- 广播迭代
- 1、标量与一维数组
- 2、标量与二维数组
- 3、二维数据与一维数组
- 4、列向量与行向量相加
- 5、二维数据与二维数组
索引访问
通过下标来访问数据元素,当总数不知道的时候,使用负值索引就很方便,特别是切片访问的时候
数组值 | 北京 | 上海 | 广州 | 深圳 |
---|---|---|---|---|
正值索引 | 0 | 1 | 2 | 3 |
负值索引 | 0 | -3 | -2 | -1 |
a = np.array([1, 2, 3, 4, 5, 6])print("| a[1]", a[1], sep=" | ", end="|\r\n")
print("| a[-1]", a[-1], sep=" | ", end="|\r\n")
print("| a[a.size-1]", a[a.size - 1], sep=" | ", end="|\r\n")
切片访问
1、
[start:end:step]
, step可以为负值,start包括,end不包括
2、通过切片返回的数组元素与原数组是一个引用,一改全改
- start可以省略,不写默认从0开始
- end可以省略,不写默认到-1结束
- step可以省略,不写默认是1
a = np.array([1, 2, 3, 4, 5, 6])
print("| a[:3:2]", "[1,3]", a[:3:2], sep=" | ", end="|\r\n")
print("| a[1::2]", "[2,4,6]", a[1::2], sep=" | ", end="|\r\n")
print("| a[::2]", "[1,3,5]", a[::2], sep=" | ", end="|\r\n")
print("| a[::]", "[1,2,3,4,5,6]", a[::], sep=" | ", end="|\r\n")
print("| a[:-2:-2]", "[1,2,3,4,5,6]", a[-2:1:-2], sep=" | ", end="|\r\n")
二维数组切片:
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
针对多维维数组,每个轴分别切片,然后取交叉点的元素
- 行(0轴),0👎1 -> 行0,1
- 列(1轴),1:4:2 ->列1,3
最终结果是3行2列的数组
a = np.array([(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)])
print(a[0:-1:1, 1:4:2])
布尔索引
1、通过一个布尔数组来索引目标数组
2、布尔数组与原数组形状必须相同
3、返回的新数组与原数组无关,可以随便修改,深层复制
4、返回数据是一个一维数组
一维数组:
a = np.arange(start=1, stop=5, step=1)
b = np.array((True, True, False, False), dtype=np.bool_)
print(a[b])
print(a[a % 2 == 0])
二维数组:
a = np.array([(1, 2, 3), (4, 5, 6)], dtype=np.int32)
b = np.array([(True, False, False), (False, True, True)], dtype=np.bool_)
print(a[b])
花式索引
1、使用整数列表、整数数组作为数组索引
2、新数组的元素属于深层复制
,修改不影响原数组
一维数组:
a = np.arange(start=1, stop=10, step=1)
# 整数数组
b = np.array([1, 2, 3])
print(a[b])# 整数列表
ll = [0, 2, 4]
print(a[ll])
二维数组:
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
- 0轴索引数组
1 | 2 |
2 | 2 |
- 1轴索引数组
0 | 0 |
1 | 1 |
- 结果展示
a[1,0] | a[2,0] |
a[2,1] | a[2,1] |
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)
# 0轴
d0 = np.array([[1, 2], [2, 2]])
# 1轴
d1 = np.array([[0, 0], [1, 1]])
print(a[d0, d1])[[4 7][8 8]]
迭代数组
使用numpy.nditer访问数组元素
1、默认遍历
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)
for x in np.nditer(a):print(x, end=", ")
print('\n')# 转置
for x in np.nditer(a.T):print(x, end=", ")
print('\n')
2、控制顺序
行优先
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)# C order,即是行序优先
for x in np.nditer(a, order='C'):print(x, end=", ")
print('\n')
列优先
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)# Fortran order,即是列序优先
for x in np.nditer(a, order='F'):print(x, end=", ")
print('\n')
3、读写模式
op_flags参数,默认
read-only
,可以设置readwrite
与writeonly
# 遍历中修改
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)for x in np.nditer(a, op_flags=['readwrite']):x[...] = 2 * x
print('修改后的数组是:')
print(a)
4、其他参数
flags参数
参数 | 描述 |
---|---|
c_index | 可以跟踪 C 顺序的索引 |
f_index | 可以跟踪 Fortran 顺序的索引 |
multi_index | 每次迭代可以跟踪一种索引类型 |
external_loop | 给出的值是具有多个值的一维数组,而不是零维数组 |
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)
it = np.nditer(a, flags=['f_index'])
for x in it:print('index:{0}, value:{1}'.format(it.index, x))
print("\n")it = np.nditer(a, flags=['c_index'])
for x in it:print('index:{0}, value:{1}'.format(it.index, x))
print("\n")it = np.nditer(a, flags=['multi_index'])
for x in it:print('index:{0}, value:{1}'.format(it.multi_index, x))
print("\n")for x in np.nditer(a, flags=['external_loop'], order='F'):print(x, end='\n')
print("\n")
广播迭代
1、数组拥有相同形状
2、当前维度的值相等
3、当前维度的值有一个是 1
1、标量与一维数组
标量与每一个元素相加
a = np.array([1, 2, 3])
b = a + 5
print(b)[6 7 8]
2、标量与二维数组
标量与每一个元素相加
a = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype=np.int32)
b = a + 5
print(b)[[ 6 7 8][ 9 10 11][12 13 14]]
3、二维数据与一维数组
这里将一维数组拉伸复制,最后对应相加
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])result = a + b
print(result)
4、列向量与行向量相加
这里分别将两个一维数组拉伸复制,最后对应相加
a = np.array([[1], [2], [3]])
b = np.array([10, 20, 30])result = a + b
print(result)
5、二维数据与二维数组
这里对应元素相加
a = np.array([(1, 2, 3), (4, 5, 6)], dtype=np.int32)
b = np.array([(10, 20, 30), (40, 50, 60)], dtype=np.int32)
print(a + b)