Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python
communitycreator
indexing

How does array indexing work using NumPy in Python?

Harsh Jain

Introduction to NumPy

NumPy performs scientific computing in Python. The NumPy library enables the user to create N-dimensional arrays and performs linear algebra operations on NumPy objects. It also offers other features, including mathematical operations such as the Fourier transform.

You can run the command below to install the NumPy library on your system:

pip install numpy

In this shot, we explore the different ways we can use array indices.

The slicing operation

The slicing operation gets some part of the data from a list, tuple, and strings. It follows the following syntax:

arr[start_pos: end_pos]

Let’s see how slicing works in the code:

from numpy import array

# Create a 1D array
one_dim_arr = array([1, 2, 3, 4, 5, 6, 7])

# Slicing operation on 1D array
sliced_one_dim_array = one_dim_arr[1:4] 
print ("Elements from the index 1 to index 4:\n", sliced_one_dim_array) 

# Create a 2D array
two_dim_arr = array([
                [1, 2, 3, 4], 
                [5, -6, 7, -8], 
                [9, 10, -11, -12]
              ]) 

# Slicing operation on 2D array
sliced_two_dim_array = two_dim_arr[1:, ::2]
print ("Array with last 2 rows and alternate columns:\n", sliced_two_dim_array) 
Use slicing operation on NumPy arrays

Explanation

  • Line 1: We import the numpy package.

  • Line 4: We create a one-dimensional NumPy array.

  • Line 7: We apply the slicing operation on the one-dimensional array. Here, we specify that we want to fetch the elements starting from index 1 to 4.

  • Lines 11 to 15: We create a two-dimensional array.

  • Line 18: We apply the slicing operation again. The difference is that we can specify the elements for the rows and columns separately. The code above specifies that we want the rows starting from index 1 by using the expression 1:. It specifies alternate columns starting from the index 0 with the expression ::2. Anything you specify after the :: expression will denote the following index from the current index.

  • Line 19: We print the sliced NumPy array.

Using integer indexing

Another way to use indices is to use an integer value. We use the value to denote the element present at the specified index in the NumPy arrays. Let’s explore how this works in the code:

from numpy import array

# Create a 1D array
one_dim_arr = array([1, 2, 3, 4, 5, 6, 7])

# Integer indexing on 1D array
integer_indexing_one_dim_array = one_dim_arr[3] 
print ("Elements at index 3:\n", integer_indexing_one_dim_array)

# Create a 2D array
two_dim_arr = array([
                [1, 2, 3, 4], 
                [5, -6, 7, -8], 
                [9, 10, -11, -12]
              ]) 

# Integer indexing on 2D array
integer_indexing_two_dim_array = two_dim_arr[[0, 1, 2], [3, 2, 1]] 
print ("Elements at indices (0, 3), (1, 2), (2, 1):\n", integer_indexing_two_dim_array)
Use integer indexing on NumPy arrays

Explanation

  • Line 1: We import the numpy package.

  • Line 4: We create a one-dimensional NumPy array.

  • Line 7: We provide the integer index 3. This is what we want to fetch from the NumPy array.

  • Lines 11 to 15: We create a two-dimensional array.

  • Line 18: We provide the integer index again. However, we specify a 2-D list because it is a two-dimensional array in this case. The first element in the list specifies the row indices. The second element specifies the column indices. The first element from the first list and the first element from the second list are considered one index. For example, in the code mentioned above, we have the indices as (0, 3), (1, 2), and (2, 1).

  • Line 19: We print the integer indexed NumPy array.

Using conditional indexing

The third way to access elements from a NumPy array is to use conditional indexing. We can provide a condition that will be checked on each element in the NumPy array. The elements that satisfy the condition are returned due to this indexing operation. Let’s see how this works in code.

from numpy import array

# Create a 1D array
one_dim_arr = array([1, 2, 3, 4, 5, 6, 7])

# conditional indexing on 1D array
boolean_indexing_one_dim_arr = one_dim_arr[one_dim_arr % 2 != 0]
print ("Odd elements:\n", boolean_indexing_one_dim_arr)

# Create a 2D array
two_dim_arr = array([
                [1, 2, 3, 4], 
                [5, -6, 7, -8], 
                [9, 10, -11, -12]
              ]) 

# Conditional indexing on 2D array
boolean_indexing_two_dim_arr = two_dim_arr[two_dim_arr % 2 == 0]
print ("Even elements:\n", boolean_indexing_two_dim_arr)
Use conditional indexing on NumPy arrays

Explanation

  • Line 1: We import the numpy package.

  • Line 4: We create a one-dimensional NumPy array.

  • Line 7: We provide a condition to check for odd elements. This condition forces us to only get the odd elements.

  • Lines 11 to 15: We create a two-dimensional array.

  • Line 18: We provide a condition again. We do this to check for the elements to be even this time. This condition will be checked for all the elements.

  • Line 19: We print the conditional indexed NumPy array that contains the even elements.

RELATED TAGS

numpy
python
communitycreator
indexing
RELATED COURSES

View all Courses

Keep Exploring