- Related Questions & Answers
- Python List Comprehension and Slicing?
- Array Indexing and Printing in Python (pyCharm)
- Boolean Indexing in Python
- Python Indexing a sublist
- Python Basic date and time types
- Alternate range slicing in list (Python)
- Broadcasting with NumPy Arrays in Python
- Basic Operators in Python
- How can I do Python Tuple Slicing?
- Differences between Flatten() and Ravel() in Numpy
- String slicing in Python to rotate a string
- Basic Python Programming Challenges
- Basic List Operations in Python
- Basic Tuples Operations in Python
- Basic Calculator II in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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.

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

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

[ 2 6 10 14 18]

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

9

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

[3 4 5 6 7 8 9]

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

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

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

**Integer array indexing:**

Let’s create a simple array with integers

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

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

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

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

Advertisements