程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python 玩轉數據 3 - NumPy ndarray Array Indexing, Slicing, Striding, View Subarray,Copy Subarray

編輯:Python

引言

主要介紹 NumPy 數組索引 Index,切片 Slicing,子數組,更多 Python 進階系列文章,請參考 Python 進階學習 玩轉數據系列

內容提要:

  1. 索引,切片,子數組概要
  2. 索引 Indexing
  3. 連續切片和跨步切片 Slicing and striding
  4. 子數組 Subarray: View vs. Copy

索引,切片,子數組概要:

索引 Indexing:

  1. Index 是一個整型數
  2. Index 從 0 開始
  3. 負數的 index 也是可以的
  4. Index 引用用中括號 [ ]
  5. 某個維度 index 的范圍值:
    正數: np.size (a, axis = axis_value) - 1
    負數: -np.size (a, axis = axis_value)
  6. 多維數組: 用逗號分開每個維度的 index

Slicing and Striding:

  1. [ start : stop : step ]: 用方括號 [ ] 表示
  2. start, stop and step 切片訪問某個特定維度的數組
  3. 負數值也是可以的
  4. 取值范圍和 index 一樣
  5. [ start, stop ): 注意 stop 是不包含的
  6. 多維數組的分片: 用逗號分開每個維度的 index

Subarray: View vs. Copy:
view subarray 會改變主 array
copy subarray 不會改變主 array

索引 Indexing

Indexing 索引主要提供隨機訪問數組的單個元素

index的重點:

  1. index: 從 0 開始
  2. 0 ≤ index ≤ np.size (a) - 1
  3. 負數 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))
# 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 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))
# 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]]

子數組 Subarray: View vs. Copy

  1. Subarray 是一個視圖不是拷貝
  2. 改變 subarray, 將會影響到 主 array
  3. 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))
# 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]]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved