[ Web Proxy ]
URL:
Viewing: https://www.pythontutorial.net/python-numpy/numpy-array-indexing/ [Back]  [Original]

NumPy Array Indexing
Skip to content

NumPy Array Indexing

Summary: in this tutorial, you’ll learn how to access elements of a numpy array using indices.

Like a list, you can use the square bracket notation ([]) to access elements of a numpy array.

NumPy array indexing on 1-D arrays #

Along a single axis, you can select elements using indices. The first element starts with index 0, the second element starts with index 1, and so on.

Besides the non-negative indices, you can use negative indices to locate elements. For example, the last element has an index -1, the second last element has an index -2, and so on.

The following example shows how to access elements of a one-dimensional array:

numpy array indexing - 1-d array [numpy array indexing - 1-d array]

import numpy as np

a = np.arange(0, 5)
print(a)

print(a[0])
print(a[1])
print(a[-1])Code language: Python (python)

Output:

[0 1 2 3 4]
0
1
4
3Code language: Python (python)

In this example:

NumPy array indexing on 2-D arrays #

With 2-D and multidimensional arrays, you can select elements as you do with 1-D arrays but for each dimension (or axis). For example:

NumPy Array Indexing - 2D array [NumPy Array Indexing - 2D array]
import numpy as np

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

print(a.shape)

print(a[0])  # [1 2 3]
print(a[1])  # [4 5 6]

print(a[0, 0])  # 1
print(a[1, 0])  # 4
print(a[0, 2])  # 3
print(a[1, 2])  # 6
print(a[0, -1])  # 3
print(a[1, -1])  # 6Code language: Python (python)

Output:

(2, 3)
[1 2 3]
[4 5 6]
1
4
3
6
3
6Code language: Python (python)

In this example, the numpy array a has the shape (2,3) therefore it has two axes:

The following explains how the array indexing works:

NumPy array indexing on 3-D arrays #

The following example creates a 3-D numpy array:

NumPy Array Indexing - 3D array [NumPy Array Indexing - 3D array]
import numpy as np

a = np.array([
    [[1, 2], [3, 4], [5, 6]],
    [[5, 6], [7, 8], [9, 10]],
])

print(a.shape)Code language: Python (python)

Output:

(2, 3, 2)Code language: Python (python)

The array has three axes.

For example:

import numpy as np

a = np.array([
    [[1, 2], [3, 4], [5, 6]],
    [[5, 6], [7, 8], [9, 10]],
])

print(a[0, 0, 1])  # 2Code language: Python (python)

The following expression returns 2:

a[0,0,1]Code language: Python (python)

The first number 0 selects the first element of the first axis so it returns:

[[1, 2], [3, 4], [5, 6]]Code language: Python (python)

The second number 0 selects the first element of the second axis so it returns:

[1, 2]Code language: Python (python)

The third number (1) selects the second element of the third axis which returns 2.

Summary #

Was this tutorial helpful ?
Yes No
Search …:

Getting Started

Creating Arrays

Array Indexing & Slicing

Aggregate Functions

Array Operations

Arithmetic Operations

Joining & Splitting Arrays

Copyright © 2021 - Present, By Python Tutorial. All Rights Reserved.


Web Proxy Viewer  |  New URL  |  Original Page