Introduction
Slicing in NumPy allows you to extract parts of arrays and create subarrays. It is a powerful feature that enables efficient manipulation and access to array data. In this chapter, you will learn the basics of slicing in NumPy, including slicing 1D, 2D, and multi-dimensional arrays.
Importing NumPy
First, import NumPy in your script or notebook:
import numpy as np
Slicing 1D Arrays
Slicing a 1D array is straightforward. The syntax is start:stop:step
.
Example: Basic Slicing
# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])
# Slicing
print(arr[1:4]) # Output: [20 30 40]
print(arr[:3]) # Output: [10 20 30]
print(arr[::2]) # Output: [10 30 50] (every second element)
Slicing 2D Arrays
Slicing a 2D array involves specifying the slice for each dimension.
Example: Slicing Rows and Columns
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Slicing rows and columns
print(arr[0:2, 1:3]) # Output: [[2 3] [5 6]]
print(arr[:, 1]) # Output: [2 5 8] (second column)
print(arr[1, :]) # Output: [4 5 6] (second row)
Example: Slicing with Steps
# Creating a 2D array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
# Slicing with steps
print(arr[::2, ::2]) # Output: [[ 1 3]
# [ 9 11]]
Slicing Multi-Dimensional Arrays
Slicing can be extended to multi-dimensional arrays by specifying the slice for each dimension.
Example: Slicing a 3D Array
# Creating a 3D array
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
# Slicing
print(arr[1:, :1, :]) # Output: [[[ 5 6]]
# [[ 9 10]]]
Negative Indexing
You can use negative indices to slice arrays from the end.
Example: Negative Indexing in a 1D Array
# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])
# Negative indexing
print(arr[-3:]) # Output: [30 40 50]
Example: Negative Indexing in a 2D Array
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Negative indexing
print(arr[-2:, -2:]) # Output: [[5 6]
# [8 9]]
Slicing and Assignment
You can modify parts of an array using slicing.
Example: Modifying Array Elements
# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])
# Modifying elements
arr[1:4] = [21, 31, 41]
print(arr) # Output: [10 21 31 41 50]
Example: Modifying a 2D Array
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Modifying elements
arr[0:2, 1:3] = [[22, 33], [55, 66]]
print(arr) # Output: [[ 1 22 33]
# [ 4 55 66]
# [ 7 8 9]]
Combining Slicing and Indexing
You can combine slicing and indexing for more complex operations.
Example: Combining Slicing and Indexing
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Combining slicing and indexing
print(arr[1:, [0, 2]]) # Output: [[4 6]
# [7 9]]
Conclusion
Slicing in NumPy allows you to efficiently access and manipulate parts of arrays. Understanding how to slice 1D, 2D, and multi-dimensional arrays, as well as using negative indexing and modifying arrays with slicing, is essential for effective numerical computing in Python.