Summary:
Indexing:
- Index 是一个整型数
- Index 从 0 开始
- 负数的 index 也是可以的
- Index 引用用中括号 [ ]
- 某个维度 index 的范围值:
正数: np.size (a, axis = axis_value) - 1 负数: -np.size (a, axis = axis_value) - 多维数组: 用逗号分开每个维度的 index
Slicing and Striding:
- [ start : stop : step ]: 用方括号 [ ] 表示
- start, stop and step 切片访问某个特定维度的数组
- 负数值也是可以的
- 取值范围和 index 一样
- [ start, stop ): 注意 stop 是不包含的
- 多维数组的分片: 用逗号分开每个维度的 index
Subarray: View vs. Copy: view subarray 会改变主 array copy subarray 不会改变主 array
Indexing
Indexing 索引主要提供随机访问数组的单个元素
index的重点:
- index: 从 0 开始
- 0 ≤ index ≤ np.size (a) - 1
- 负数 index is ok
–index ≡ np.size (a) — index
index 范围:
举例:
import numpy as np
array_1_d = np.arange(1, 5)
array_2_d = np.random.randint (0, 100, (3, 4))
row = np.size (array_2_d, axis=0) - 1
col = np.size (array_2_d, axis=1) - 1
right_most = array_2_d[row, col]
print("array_1_d: {}\nfirst element array_1_d[0]: {}\nlast element array_1_d[-1]: {} ".format(array_1_d, array_1_d[0], array_1_d[-1]))
print("array_2_d:\n{} \nfirst element array_2_d[0][0]: {}\nlast element array_2_d[-1][-1]: {} ".format(array_2_d, array_2_d[0][0], array_2_d[-1][-1]))
print("last element array_2_d[row, col]: {}".format(right_most))
输出:
array_1_d: [1 2 3 4]
first element array_1_d[0]: 1
last element array_1_d[-1]: 4
array_2_d:
[[15 53 42 77]
[48 38 67 53]
[12 84 98 17]]
first element array_2_d[0][0]: 15
last element array_2_d[-1][-1]: 17
last element array_2_d[row, col]: 17
Slicing and striding 连续切片和跨步切片
分片访问数组的元素
Slicing 连续切片 注意:end_index是不包含的 subarray = array [ start_index : end_index ] np.size (subarray) = end_index – start_index 连续切片举例:
import numpy as np
array_1 = np.arange(0,5)
array_2 = np.arange(5, 12)
array_join = np.concatenate([array_1, array_2])
subarray_1 = array_join[1:5]
array_2_d = np.reshape (array_join, (3,4))
subarray_2 = array_2_d[:2, :3]
first_column = array_2_d[:, 0]
thrid_row = array_2_d[2, :]
print("array_1:{}\n".format(array_1))
print("array_2:{}\n".format(array_2))
print("array_join:{}\n".format(array_join))
print("sub array of array_join array_join[1:5]:{}\n".format(subarray_1))
print("array_2_d:{}\n".format(array_2_d))
print("sub array of array_2_d array_2_d[:2, :3]:{}\n".format(subarray_2))
print("the first column of array_2_d array_2_d[:, 0]{}\n".format(first_column))
print("the third row of array_2_d array_2_d[2, :]:{}\n".format(thrid_row))
输出:
array_1:[0 1 2 3 4]
array_2:[ 5 6 7 8 9 10 11]
array_join:[ 0 1 2 3 4 5 6 7 8 9 10 11]
sub array of array_join array_join[1:5]:[1 2 3 4]
array_2_d:[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
sub array of array_2_d array_2_d[:2, :3]:[[0 1 2]
[4 5 6]]
the first column of array_2_d array_2_d[:, 0][0 4 8]
the third row of array_2_d array_2_d[2, :]:[ 8 9 10 11]
一维数组跨步不连续切片: subarray = array [ start_index : end_index : stride ]
多维数组跨步不连续切片: subarray = m [ start_0 : end_0 : stride_0, start_1 : end_1 : stride_1 ] 跨步不连续切片举例:
import numpy as np
array_1 = np.arange(0,12)
array_2_d = np.reshape (array_1, (3,4))
subarray_1_every_other = array_1[::2]
subarray_1_every_other_start_from_1 = array_1[1::2]
subarray_1_reversed = array_1[::-1]
subarray_2_d_reversed = array_2_d[::-1, ::-1]
print("array_1:{}\n".format(array_1))
print("subarray_1_every_other -> array_1[::2]:{}\n".format(subarray_1_every_other))
print("subarray_1_every_other_start_from_1 -> array_1[1::2]:{}\n".format(subarray_1_every_other_start_from_1))
print("subarray_1_reversed -> array_1[::-1]:{}\n".format(subarray_1_reversed))
print("array_2_d:{}\n".format(array_2_d))
print("subarray_2_d_reversed -> array_2_d[::-1, ::-1]:\n{}".format(subarray_2_d_reversed))
输出:
array_1:[ 0 1 2 3 4 5 6 7 8 9 10 11]
subarray_1_every_other -> array_1[::2]:[ 0 2 4 6 8 10]
subarray_1_every_other_start_from_1 -> array_1[1::2]:[ 1 3 5 7 9 11]
subarray_1_reversed -> array_1[::-1]:[11 10 9 8 7 6 5 4 3 2 1 0]
array_2_d:[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
subarray_2_d_reversed -> array_2_d[::-1, ::-1]:
[[11 10 9 8]
[ 7 6 5 4]
[ 3 2 1 0]]
Subarray: View vs. Copy
- Subarray is 是一个视图不是拷贝
- 改变 subarray, 将会影响到 主 array
- 用 np.copy() 方法创建一个主 array 的拷贝,这样改变subarray 的值就不会影响到主 array
举例:
import numpy as np
a = np.arange (12)
ma = np.reshape (a, (3,4))
print("main array:\n{}\n".format(ma))
sma = ma [ : 2, : 2]
print("sub view array -> ma [ : 2, : 2]:\n{}\n".format(sma))
sma [0, 0] = 100
print("the changed sub view array -> sma [0, 0] = 100:\n{}\n".format(sma))
print("the changed main array:\n{}\n".format(ma))
cma = np.copy (sma)
print("sub copy array -> np.copy (sma):\n{}\n".format(cma))
cma [1, 1] = 1000
print("the changed sub copy array:\n{}\n".format(cma))
print("main array not changed:\n{}\n".format(ma))
输出:
main array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
sub view array -> ma [ : 2, : 2]:
[[0 1]
[4 5]]
the changed sub view array -> sma [0, 0] = 100:
[[100 1]
[ 4 5]]
the changed main array:
[[100 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
sub copy array -> np.copy (sma):
[[100 1]
[ 4 5]]
the changed sub copy array:
[[ 100 1]
[ 4 1000]]
main array not changed:
[[100 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
|