Indexing and Slicing

Array indexing refers to any use of the square brackets ([]) to index array values. There are many options to indexing, which give Numpy indexing great power. We are going to discuss the methods one by one.
1. Single element indexing: Single element indexing is about access element as we do for the 1-D array in the normal python script.
Unlike lists and tuples, Numpy arrays support multidimensional indexing for multidimensional arrays. That means that it is not necessary to separate each dimension’s index into its own set of square brackets. Let’s get it into the head through an example as one should be good at indexing, it might help a lot.

Single element indexing

import numpy as np
##Let's first create 1-D array
x = np.arange(10)
print(x)
print(x[0])
[0 1 2 3 4 5 6 7 8 9]
0
print(x[1])
1

Converting to multidimensional array

x.shape = (2,5)
## Now it is multidimensional array of shape-> (2,5)
print(x)
print(x[0])
[[0 1 2 3 4]
 [5 6 7 8 9]]
[0 1 2 3 4]

See the different method for indexing element in multidimensional

print(x[1,2])
print(x[1][2])
7
7
2. Slice indexing: Through slicing and striding, we can access the elements as we do for list and tuples in
python. Let’s get some illustrations of it.

How to do the slicing

The format of slicing (start index: end index: stride value) stride value-> skipping how many values

import numpy as np

x = np.arange(10)
print(x[2:5])
print(x[2:5:2])
[2 3 4]
[2 4]

For multidimensional array

y = np.arange(35).reshape(5,7)
print(y)
[[ 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 25 26 27]
 [28 29 30 31 32 33 34]]

Syntax explanation for multidimensional- [index of row, index of col]Index of row- It can be a single value or with slicing and stride (similar for the index of col)

## Simple without stride and slice
print(y[1,2])
9

Using “:” will go from start to end

## Without stride
print("This is without stride-:")
print(y[:])

print("\n")
## With stride
print("This is with the stride of 3-:")
print(y[::3])
This is without stride-:
[[ 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 25 26 27]
 [28 29 30 31 32 33 34]]


This is with the stride of 3-:
[[ 0  1  2  3  4  5  6]
 [21 22 23 24 25 26 27]]
## It will extract rows from 1 to 4 and for all columns
print(y[1:5,:])
[[ 7  8  9 10 11 12 13]
 [14 15 16 17 18 19 20]
 [21 22 23 24 25 26 27]
 [28 29 30 31 32 33 34]]
## If we want to choose particular columns
print(y[1:5:2,::3])
[[ 7 10 13]
 [21 24 27]]
3. Index Arrays: We can access elements of a Numpy array using another array(or any other sequence – like object that can be converted to an array, such as lists, with the exception of tuples)

How to do indexing through arrays

import numpy as np

x = np.arange(10,1,-1)
print(x)
[10  9  8  7  6  5  4  3  2]
print(x[np.array([3,3,4,7])])
[7 7 6 3]

Negative indexing is the same as work with single indexes

x[np.array([3,3,-3,8])]
array([7, 7, 4, 2])

Index out of range will give an error

x[np.array([3,3,19,8])]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-9eeb5decb0c8> in <module>()
----> 1 x[np.array([3,3,19,8])]

IndexError: index 19 is out of bounds for axis 1 with size 9
x[np.array([[1,1],[2,3]])]
array([[9, 9],
       [8, 7]])
4. Boolean index: Boolean indexing is used when we need to do some comparisons between elements of Numpy array. To illustrate:

How to use boolean indexing

import numpy as np
x = np.arange(35)
print(x)
[ 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 25 26 27 28 29 30 31 32 33 34]

If we want elements having value >20

## Comparision operator should be used first and it will return boolean array.
b = x>20
print(b)
## It will return elements which are having True value in b
print(x[b])

## We can merge these 2 steps in one step
print(x[x>20])
[False False False False False False False False False False False False
 False False False False False False False False False  True  True  True
  True  True  True  True  True  True  True  True  True  True  True]
[21 22 23 24 25 26 27 28 29 30 31 32 33 34]
[21 22 23 24 25 26 27 28 29 30 31 32 33 34]

Leave a Comment

Your email address will not be published. Required fields are marked *