NumPy: Index + Slice

NumPy: Index + Slice

Article Directory

Introduction

Previous: NumPy: Start + Create Array .Now that we've been able to build multidimensional arrays, we'll show you how to index and slice, simply by selecting specific elements or elements within a specified range.

Reference

NumPy slices and indexes https://www.runoob.com/numpy/numpy-ndexing-and-slicing.html

text

One-dimensional array

Before explaining numpy's powerful indexing and slicing capabilities, let's review the most basic way to index one-dimensional arrays:

import numpy as np

a = np.arange(10)

# Select specific elements
print(a[3])  # 3

# Select a specific range
print(a[1:7]) # [1 2 3 4 5 6]

# Use built-in function slice
print(a[slice(1,7,2)]) # [1 3 5]

# Incoming index array, numpy specific
print(a[[2,4,6]]) # [2 4 6]

The parameters of the slice index are equivalent to the built-in function slice, one is [start:end:step], one is slice(start,end,step); and the last is a unique writing of numpy through the incoming index array

Multidimensional Array

Next, we can expand to the index and slice of multidimensional arrays. Each dimension's index can use four indexing methods of one-dimensional arrays, using between multiple dimensions, separating, and using.. ellipsis as the default (that is, selecting all, equivalent to [:]):

import numpy as np

# Original Array
a = np.arange(25).reshape(5,5)
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]
#  [15 16 17 18 19]
#  [20 21 22 23 24]]

# Select specific elements
print(a[2][3])
# 13
print(a[3][2])
# 17

# Select Limited Range
print(a[1: , :2])
# [[ 5  6]
#  [10 11]
#  [15 16]
#  [20 21]]
print(a[..., :3])
# [[ 0  1  2]
#  [ 5  6  7]
#  [10 11 12]
#  [15 16 17]
#  [20 21 22]]

# Incoming Index Array
print(a[[2,4], 0:5:2])
# [[10 12 14]
#  [20 22 24]]
print(a[...,3])
# [ 3  8 13 18 23]

We use a two-dimensional array to draw instructions

  • Original Array

  • Indexes

  • Section

  • Index Array

Is it easy?

Other indexes

In addition to the basic four indexes and their use to combine multidimensional indexes, here are two other types of indexes

Boolean Index

Indexing by a Boolean expression will traverse the entire array as if you were using the map function to finally aggregate a one-dimensional array

import numpy as np

# Original Array
a = np.arange(25).reshape(5,5)
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]
#  [15 16 17 18 19]
#  [20 21 22 23 24]]

# Using Boolean expressions (comparison logical operators)
print(a[7 <= a])
# [7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]

# Use ~non-operator
print(a[~(a < 7)])
# [7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]

order arrangement

Finally, let's explainNp.arrayThe meaning of the order parameter in, I believe that people who read the previous article are still confused, and I am also hhh, which will be explained in detail once.

The original C language array is row-first, and the memory model is as follows:

a = np.arange(9).reshape(3,3)

+---------+
| a[0][0] |
+---------+
| a[0][1] |
+---------+
| a[0][2] |
+---------+
| a[1][0] |
+---------+
| a[1][1] |
+---------+
| a[1][2] |
+---------+
| a[2][0] |
+---------+
| a[2][1] |
+---------+
| a[2][2] |
+---------+

Fortran language arrays are column-first, with the following memory

b = a.copy(order='F')

+---------+
| b[0][0] |
+---------+
| b[1][0] |
+---------+
| b[2][0] |
+---------+
| b[0][1] |
+---------+
| b[1][1] |
+---------+
| b[2][1] |
+---------+
| b[0][2] |
+---------+
| b[1][2] |
+---------+
| b[2][2] |
+---------+

However, this has no effect on the array index, only using an iterator (np.nditer) The order is affected when traversing:

import numpy as np

a = np.arange(9).reshape(3,3)
b = a.copy(order='F')

# Original array, you can see that order does not affect the element index of the entire array
print(a)
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
print(b)
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]

# Traversing through the array in rows and columns, you can see that the results are the same
print('\na:')
for row in a:
  for col in row:
    print(col, end=",")
# a:
# 0,1,2,3,4,5,6,7,8,

print('\nb:')
for row in b:
  for col in row:
    print(col, end=",")
# b:
# 0,1,2,3,4,5,6,7,8,

# When iterators are used, however, they are traversed in the order of the memory model, that is, order='F'takes the form of row precedence
print('\na by nditer:')
for x in np.nditer(a):
  print(x, end=",")
# a by nditer:
# 0,1,2,3,4,5,6,7,8,

print('\nb by nditer:')
for x in np.nditer(b):
  print(x, end=",")
# b by nditer:
# 0,3,6,1,4,7,2,5,8,

Conclusion

This article describes how multidimensional arrays are indexed in numpy. Slices and Boolean indexes provide a powerful ability to find elements, enabling developers to easily specify the target range for extracting arrays.In the next article, we will begin to introduce one of numpy's most powerful capabilities: the multidimensional array operation function.

Tags: C

Posted on Wed, 24 Jun 2020 12:59:59 -0400 by suaji