
NumPy array indexing and slicing is a powerful feature that allows you to access and manipulate the elements of an array in various ways. In this tutorial, we will introduce the basics of array indexing and slicing in NumPy, and show you how to use these techniques to access and manipulate the elements of an array.
One of the most basic forms of array indexing is selecting a single element from an array. To do this, you can use the square brackets []
operator and specify the indices of the element you want to select. For example, given an array a
with shape (3, 4), you can select the element at row 0 and column 1 using the index [0, 1]
.
In addition to selecting single elements, you can also use array indexing to select a slice of elements from an array. A slice is a subset of elements from an array, and you can create a slice by specifying a start index, an end index, and a step size. For example, given an array a
with shape (3, 4), you can create a slice that includes all the elements from row 1 to row 2 using the indices [1:3]
.
Array slicing is a powerful and flexible feature of NumPy, and it is an essential tool for working with arrays of data. In the following sections, we will take a closer look at how to use array indexing and slicing in NumPy, and see some examples of these techniques in action.
NumPy Array Indexing
NumPy array indexing is a method of accessing and manipulating the elements of an array. It allows you to select and manipulate specific elements or slices of elements from an array. NumPy array indexing is a powerful and flexible feature that is an essential part of working with arrays in NumPy.
NumPy array indexing is used for a wide variety of purposes, such as:
- Accessing and manipulating individual elements of an array: You can use array indexing to access and manipulate specific elements of an array. For example, given an array
a
with shape (3, 4), you can select the element at row 0 and column 1 using the index[0, 1]
. - Selecting and manipulating slices of an array: You can use array indexing to select and manipulate slices of an array, which are subsets of the elements in the array. For example, given an array
a
with shape (3, 4), you can create a slice that includes all the elements from row 1 to row 2 using the indices[1:3]
. - Modifying the values of elements in an array: You can use array indexing to modify the values of specific elements or slices of elements in an array. For example, given an array
a
with shape (3, 4), you can set the value of the element at row 0 and column 1 to 0 using the index[0, 1]
.
Here are a few examples of using array indexing in NumPy:
import numpy as np
# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
# Select the element at row 0 and column 1
print(a[0, 1])
# Output: 2
# Select a slice of elements from row 1 to row 2
print(a[1:3])
# Output: [[ 5 6 7 8]
# [ 9 10 11 12]]
# Modify the value of the element at row 0 and column 1
a[0, 1] = 0
print(a)
# Output: [[ 1 0 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
NumPy Array Slicing
To slice a NumPy array is to select a subset of elements from the array, which is known as a slice. NumPy array slicing is a powerful and flexible feature that allows you to access and manipulate the elements of an array in various ways.
A good use case for NumPy array slicing is selecting and manipulating a contiguous group of elements from an array. For example, you might want to select all the elements from a certain row or column of a 2-D array, or select a range of elements from a 1-D array. NumPy array slicing makes it easy to do this by allowing you to specify a start index, an end index, and a step size.
Here is an example of using NumPy array slicing to select and manipulate a slice of elements from an array:
import numpy as np
# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
# Select a slice of elements from row 1 to row 2
b = a[1:3]
print(b)
# Output: [[ 5 6 7 8]
# [ 9 10 11 12]]
# Modify the values of the slice
b[:, 1:3] = 0
print(b)
# Output: [[5 0 0 8]
# [9 0 0 12]]
# Notice that the original array was also modified
print(a)
# Output: [[ 1 2 3 4]
# [ 5 0 0 8]
# [ 9 0 0 12]]
As you can see, NumPy array slicing is a useful and powerful tool for accessing and manipulating the elements of an array.
NumPy Array Subsetting
NumPy array subsetting is a technique for selecting and manipulating a subset of elements from an array. NumPy provides several methods for subsetting arrays, including indexing, slicing, and boolean indexing.
You might use NumPy array subsetting in a variety of scenarios, such as:
- Accessing and manipulating individual elements of an array: You can use indexing to select specific elements of an array, or use slicing to select a range of elements.
- Selecting elements based on a condition: You can use boolean indexing to select elements of an array based on a condition, such as selecting all elements that are greater than a certain value.
- Modifying the values of elements in an array: You can use subsetting techniques to modify the values of specific elements or slices of elements in an array.
Here are a few examples of using NumPy array subsetting:
import numpy as np
# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
# Select the element at row 0 and column 1
print(a[0, 1])
# Output: 2
# Select a slice of elements from row 1 to row 2
print(a[1:3])
# Output: [[ 5 6 7 8]
Summary
In summary, NumPy array indexing and slicing are techniques for accessing and manipulating the elements of an array. Array indexing allows you to select and manipulate specific elements of an array, while array slicing allows you to select and manipulate a slice of elements from an array. NumPy array indexing and slicing are powerful and flexible tools for working with arrays of data, and they are an essential part of the NumPy library.
- NumPy Array Indexing and Slicing (vegibit.com)
- Indexing on ndarrays — NumPy v1.24 Manual (numpy.org)
- Indexing and Slicing NumPy Arrays: A Complete Guide • datagy (datagy.io)
- Indexing and Slicing NumPy Arrays – Scaler Topics (www.scaler.com)
- How to slice a numpy array by a list of column indices (stackoverflow.com)
- PythonInformer – Indexing and slicing numpy arrays (www.pythoninformer.com)
- NumPy – Indexing & Slicing – TutorialsPoint (www.tutorialspoint.com)
- Indexing and Slicing of 1D, 2D, and 3D Arrays in Numpy (towardsdatascience.com)
- Array Indexing and Slicing — Introduction to NumPy – Data Journal (hossainlab.github.io)
- Numpy Array Slicing – Python Tutorial (www.pythontutorial.net)
- Accessing elements of a Numpy Array through Slicing (www.h2kinfosys.com)
- NumPy Array Indexing and Slicing – Python Wife (pythonwife.com)
- Numpy Array Indexing & Slicing – Medium (medium.com)
- NumPy Array Indexing And Slicing – The Click Reader (www.theclickreader.com)
- Python NumPy Indexing and Slicing – Studytonight (www.studytonight.com)
- Slice (or Select) Data From Numpy Arrays – Earth Data Science (www.earthdatascience.org)
- Numpy’s indexing and slicing notation explained visually (wbuchmueller.medium.com)
- How to Index, Slice and Reshape NumPy Arrays for Machine Learning (machinelearningmastery.com)