Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
In NumPy, you can select slices of an array along specific axes using standard slicing techniques combined with the take()
function or using basic slice notation.
Let's walk through some examples:
Start by importing NumPy:
import numpy as np
For a 2D array:
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get the first row (axis 0) print(arr[0, :]) # Output: [1 2 3] # Get the first column (axis 1) print(arr[:, 0]) # Output: [1 4 7]
take()
function:The take()
function allows you to specify indices and an axis:
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get the first and third row (along axis 0) rows = np.take(arr, indices=[0, 2], axis=0) print(rows) # Output: # [[1 2 3] # [7 8 9]] # Get the first and third column (along axis 1) cols = np.take(arr, indices=[0, 2], axis=1) print(cols) # Output: # [[1 3] # [4 6] # [7 9]]
For a 3D array:
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Get the first matrix (along axis 0) print(arr_3d[0, :, :]) # Output: # [[1 2] # [3 4]] # Get the first row of each matrix (along axis 1) print(arr_3d[:, 0, :]) # Output: # [[1 2] # [5 6]] # Get the first column of each matrix (along axis 2) print(arr_3d[:, :, 0]) # Output: # [[1 3] # [5 7]]
Slicing arrays in NumPy is a fundamental skill that allows you to access and manipulate specific portions of your data efficiently. Both basic slice notation and the take()
function offer flexible ways to get slices along the desired axes.
Description: NumPy allows you to select specific slices along a particular axis in a multidimensional array.
Code:
import numpy as np # Create a 2D NumPy array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Select slices along the second axis slices_along_axis = array_2d[:, 1] print("Original Array:") print(array_2d) print("Selected Slices along Axis 1:") print(slices_along_axis)
Description: You can extract specific slices from a NumPy array using slicing notation.
Code:
import numpy as np # Create a 1D NumPy array array_1d = np.array([1, 2, 3, 4, 5]) # Extract specific slices selected_slices = array_1d[1:4] print("Original Array:") print(array_1d) print("Selected Slices:") print(selected_slices)
Description: NumPy supports indexing and slicing along specific axes, allowing you to access and manipulate array elements.
Code:
import numpy as np # Create a 2D NumPy array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Indexing and slicing along the second axis selected_elements = array_2d[:, 1:3] print("Original Array:") print(array_2d) print("Selected Elements along Axis 1:") print(selected_elements)
Description: Advanced array slicing in NumPy allows you to extract non-contiguous elements or sections from an array.
Code:
import numpy as np # Create a 1D NumPy array array_1d = np.array([1, 2, 3, 4, 5]) # Advanced slicing to extract non-contiguous elements selected_elements = array_1d[[1, 3, 4]] print("Original Array:") print(array_1d) print("Selected Elements using Advanced Slicing:") print(selected_elements)
Description: In a 3D array, you can slice along the third axis to extract specific sections.
Code:
import numpy as np # Create a 3D NumPy array array_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) # Slicing along the third axis selected_sections = array_3d[:, :, 1] print("Original Array:") print(array_3d) print("Selected Sections along Axis 2:") print(selected_sections)
Description: You can extract specific sections from a multi-dimensional array using slicing notation.
Code:
import numpy as np # Create a 2D NumPy array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Extract specific sections using slicing selected_sections = array_2d[1:3, 0:2] print("Original Array:") print(array_2d) print("Selected Sections:") print(selected_sections)
Description: NumPy array slicing notation along a specific axis allows you to efficiently extract sections.
Code:
import numpy as np # Create a 2D NumPy array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Slice notation along the second axis selected_slices = array_2d[:, 1:3] print("Original Array:") print(array_2d) print("Selected Slices along Axis 1:") print(selected_slices)
Description: Indexing and retrieving slices along a specific axis in NumPy allows you to access elements or sections efficiently.
Code:
import numpy as np # Create a 2D NumPy array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Indexing and retrieving slices along the second axis selected_slices = array_2d[:, 1:3] print("Original Array:") print(array_2d) print("Selected Slices along Axis 1:") print(selected_slices)
Description: You can get specific slices from a NumPy array along a given axis using slicing notation.
Code:
import numpy as np # Create a 2D NumPy array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get specific slices along the first axis selected_slices = array_2d[1:3, :] print("Original Array:") print(array_2d) print("Selected Slices along Axis 0:") print(selected_slices)