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

Python playing with data 3 - numpy ndarray array indexing, slicing, striding, view subarray, copy subarray

編輯:Python

introduction

This paper mainly introduces NumPy Array index Index, section Slicing, Subarray , more Python Advanced Series , Please refer to Python Advanced learning Play data series

Summary :

  1. Indexes , section , Subarray summary
  2. Indexes Indexing
  3. Serial slice and step slice Slicing and striding
  4. Subarray Subarray: View vs. Copy

Indexes , section , Subarray summary :

Indexes Indexing:

  1. Index It's an integer
  2. Index from 0 Start
  3. negative index It's OK, too
  4. Index Quotation is in brackets [ ]
  5. Some dimension index Range value of :
    Positive numbers : np.size (a, axis = axis_value) - 1
    negative : -np.size (a, axis = axis_value)
  6. Multidimensional arrays : Separate the... Of each dimension with commas index

Slicing and Striding:

  1. [ start : stop : step ]: In square brackets [ ] Express
  2. start, stop and step Slices access an array of a particular dimension
  3. Negative values are OK
  4. The range and index equally
  5. [ start, stop ): Be careful stop Is not included
  6. Slicing of multidimensional arrays : Separate the... Of each dimension with commas index

Subarray: View vs. Copy:
view subarray Will change the master array
copy subarray Will not change the Lord array

Indexes Indexing

Indexing The index mainly provides random access to a single element of an array

index Key points of :

  1. index: from 0 Start
  2. 0 ≤ index ≤ np.size (a) - 1
  3. negative index is ok
    –index ≡ np.size (a) — index

index Range :

give an example :

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))

Output :

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

Serial slice and step slice Slicing and striding

Accessing the elements of an array in slices

Slicing Serial sections
Be careful :end_index Is not included
subarray = array [ start_index : end_index ]
np.size (subarray) = end_index – start_index


Examples of serial slices :

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))

Output :

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]

One dimensional array step discontinuous slice :
subarray = array [ start_index : end_index : stride ]

Multi dimensional array step discontinuous slice :
subarray = m [ start_0 : end_0 : stride_0, start_1 : end_1 : stride_1 ]

Step discontinuity slice example :

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))

Output :

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 Subarray: View vs. Copy

  1. Subarray Is a view, not a copy
  2. change subarray, Will affect Lord array
  3. use np.copy() Method to create a master array A copy of the , Change like this subarray The value of will not affect the main array

give an example :

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))

Output :

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