主要介紹 NumPy 數組索引 Index,切片 Slicing,子數組,更多 Python 進階系列文章,請參考 Python 進階學習 玩轉數據系列
內容提要:
索引 Indexing:
Slicing and Striding:
Subarray: View vs. Copy:
view subarray 會改變主 array
copy subarray 不會改變主 array
Indexing 索引主要提供隨機訪問數組的單個元素
index的重點:
index 范圍:
舉例:
import numpy as np
array_1_d = np.arange(1, 5)
array_2_d = np.random.randint (0, 100, (3, 4))
# Right-most element of the 2-d array
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 連續切片注意: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))
# every other element
subarray_1_every_other = array_1[::2]
# every other element, starting at index 1
subarray_1_every_other_start_from_1 = array_1[1::2]
# reversed array
subarray_1_reversed = array_1[::-1]
# reversing all dimensions
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]]
舉例:
import numpy as np
a = np.arange (12)
ma = np.reshape (a, (3,4))
print("main array:\n{}\n".format(ma))
# a view not a copy
sma = ma [ : 2, : 2]
print("sub view array -> ma [ : 2, : 2]:\n{}\n".format(sma))
# changing the view, changes the original array
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))
# create a copy of the subarray
cma = np.copy (sma)
print("sub copy array -> np.copy (sma):\n{}\n".format(cma))
# changing the copy, do not change the original array
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]]