Basic Slicing and Advanced Indexing in NumPy Python

PythonServer Side ProgrammingProgramming

Indexing of ndarrays can be done using the standard python x[obj] syntax, where x is the array and obj the selection.

There are three kinds of indexing available −

  • field access
  • basic slicing
  • advanced indexing

What kind of indexing will be there depends on obj. In this section, we are going to mainly concentrate on basic slicing & advanced indexing.

We can divide advanced indexing into two parts −

  • Integer array indexing
  • Boolean indexing

Basic Slicing

Python basic concept of slicing is extended in basic slicing to n dimensions. As with python slice object which is constructed by giving a start, stop & step parameters to slice function. To get specific output, the slice object is passed to the array to extract a part of an array.

Example 1

import numpy as np
arr = np.arange(25)
s = slice(2, 21, 4)
print (arr[s])

Output

[ 2 6 10 14 18]

In the above example, we first created a ndarray object (arr) using arange() function. Then a slice object is created by assigning start, stop and step value to it (s). When we passed the slice object to the ndarray, we get part (slice) of the array starting with index 2 up to 21 with a step of 4.

Another way to write the above program,

# Another way to write above program
import numpy as np
arr = np.arange(25)
s = arr[2:21:4]
print (s)

Output

[ 2 6 10 14 18]

Slice single item

#Slice single item from an array
import numpy as np
arr = np.arange(10)
s = arr[9]
print(s)

Output

9

Slice items starting from index

#slice item starting from index
import numpy as np
arr = np.arange(10)
s = arr[3:]
print(s)

Output

[3 4 5 6 7 8 9]

Slice item between indexes

#slice item between indexes
import numpy as np
arr = np.arange(10)
s = arr[3: 7]
print(s)

Output

[3 4 5 6]

Above two methods will be applied to multi-dimensional ndarray too, like below −

#slice item between indexes
import numpy as np
arr = np.array([[[1],[2],[3]], [[4],[5],[6]], [[7], [8], [9]]])
s = arr[1:]
print(s)

Output

[[[4]
[5]
[6]]
[[7]
[8]
[9]]]

Advanced indexing

Integer array indexing:

Let’s create a simple array with integers

arr=np.array([[1,2],[3,4],[5,6]])
print(arr)

Output

[[1 2]
[3 4]
[5 6]]

Let’s try to select a specific element from the array, like elements with row index [0, 1, 2] and column index [1, 0, 1] from the multidimensional ndarray.

import numpy as np
arr=np.array([[1,2],[3,4],[5,6]])
s = arr[[0, 1, 2],[1, 0, 1]]
print(s)

Output

[2 3 6]

Selecting with 0 indexes will give you first row −

>>> arr=np.array([[1,2],[3,4],[5,6]])
>>> print(arr[0])
[1 2]

Similarly, we can select a single item from the array, for example- select the 1 as row index and 1 as the column index element which gives an array value of 4.

>>> print(arr[[1], [1]])
[4]

We can arithmetic operation like addition and returns the value of a particular index after performing the addition.

>>> print(arr[[1], [1]]+ 1)
[5]

As we can see the index value is incremented by 1 but the actual array remains the same.

>>> arr
array([[1, 2],
[3, 4],
[5, 6]])

But we can change the values of the array and returns the new copy of an array.

>>> arr[[1], [1]] +=1
>>> arr
array([[1, 2],
[3, 5],
[5, 6]])

Boolean indexing

We used boolean indexing when the result is going to be the outcome of boolean operations.

>>> arr=np.array([[0,1,2], [3,4,5], [6,7,8], [9,10,11]])
>>> arr
array([[ 0, 1, 2],
   [ 3, 4, 5],
   [ 6, 7, 8],
   [ 9, 10, 11]])

Returns the values which are 1.

>>> arr[arr == 1]
array([1])

Returns the values which are even numbers

>>> arr[arr %2 == 0]
array([ 0, 2, 4, 6, 8, 10])
raja
Published on 20-Feb-2019 12:44:00
Advertisements