Introduction to Python NumPy Array Slicing
Python NumPy Array Slicing
Array slicing refers to extracting a portion of an array by specifying a range of indices or using a specific pattern.
Slicing allows you to work with subsets of the original array without creating a copy, which can be useful for manipulating and analyzing data efficiently.
Here's how you can perform array slicing in NumPy:
One-Dimensional Arrays
For a one-dimensional array, slicing is similar to Python lists. You can use the colon :
notation to specify the start, stop, and step size.
import numpy as np
# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Slice a portion of the array
sliced_arr = arr[2:7] # Elements from index 2 to 6 (7 is exclusive)
print(sliced_arr) # Output: [3 4 5 6 7]
# Slice with a step size
step_arr = arr[1:9:2] # Elements from index 1 to 8 with step size 2
print(step_arr) # Output: [2 4 6 8]
# Slice from the beginning to a specific index
start_arr = arr[:5] # Elements from the beginning to index 4 (5 is exclusive)
print(start_arr) # Output: [1 2 3 4 5]
# Slice from a specific index to the end
end_arr = arr[6:] # Elements from index 6 to the end
print(end_arr) # Output: [7 8 9 10]
# Slice with negative indexing
neg_arr = arr[-5:-2] # Elements from the fifth last element to the third last element
print(neg_arr) # Output: [6 7 8]
Multi-Dimensional Arrays
For multi-dimensional arrays, you can slice along each dimension separately using the comma ,
notation.
import numpy as np
# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Slice rows and columns
sliced_rows = arr[1:3, :] # Rows 1 and 2, all columns
print(sliced_rows)
# Output:
# [[4 5 6]
# [7 8 9]]
sliced_cols = arr[:, 1:] # All rows, columns 1 and onwards
print(sliced_cols)
# Output:
# [[2 3]
# [5 6]
# [8 9]]
# Combine row and column slicing
sliced_subset = arr[0:2, 1:] # Rows 0 and 1, columns 1 and onwards
print(sliced_subset)
# Output:
# [[2 3]
# [5 6]]
Negative Slicing
Negative slicing in NumPy allows you to extract elements from an array using negative indices.
It provides a convenient way to access elements from the end of the array while specifying a range or pattern.
Here's how you can perform negative slicing in NumPy:
One-Dimensional Arrays
For a one-dimensional array, negative slicing works similar to positive slicing using the colon :
notation.
import numpy as np
# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Negative slicing
negative_slice = arr[-6:-2] # Elements from the sixth last element to the third last element
print(negative_slice) # Output: [5 6 7 8]
In the example above:
- The negative slice
[-6:-2]
corresponds to elements starting from the sixth last element (index -6) up to the third last element (index -2).
Multi-Dimensional Arrays
For multi-dimensional arrays, negative slicing can be applied along each dimension using negative indices.
import numpy as np
# Create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Negative slicing
negative_slice = arr[-2:, :-1] # Last two rows, all columns except the last one
print(negative_slice)
# Output:
# [[4 5]
# [7 8]]
In the example above:
- The negative slice
[-2:, :-1]
corresponds to the last two rows (index -2 onwards) and all columns except the last one (index :-1).