Sorting in NumPy can be done in-place or by creating a new sorted array. There are mainly two functions for sorting: np.sort()
and np.ndarray.sort()
.
np.sort()
returns a new sorted array, leaving the original array unchanged.np.ndarray.sort()
sorts the array in-place, modifying the original array directly.The sorting can be done along a specific axis in multi - dimensional arrays. By default, sorting is done along the last axis.
import numpy as np
# Create a 1D array
arr_1d = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
# Using np.sort()
sorted_arr_1d = np.sort(arr_1d)
print("Original 1D array:", arr_1d)
print("Sorted 1D array using np.sort():", sorted_arr_1d)
# Using np.ndarray.sort()
arr_1d.sort()
print("Original 1D array after in-place sorting:", arr_1d)
# Create a 2D array
arr_2d = np.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
# Sort along the rows (axis = 1)
sorted_arr_2d_rows = np.sort(arr_2d, axis=1)
print("Original 2D array:")
print(arr_2d)
print("Sorted 2D array along rows:")
print(sorted_arr_2d_rows)
# Sort along the columns (axis = 0)
sorted_arr_2d_cols = np.sort(arr_2d, axis=0)
print("Sorted 2D array along columns:")
print(sorted_arr_2d_cols)
np.ndarray.sort()
as it modifies the original array. If you need to keep the original array intact, use np.sort()
.Searching in NumPy arrays can be done to find the indices of elements that satisfy a certain condition. The main functions for searching are np.where()
, np.argmax()
, and np.argmin()
.
np.where()
returns the indices of elements in an array that satisfy a given condition.np.argmax()
returns the index of the maximum value in an array, and np.argmin()
returns the index of the minimum value.import numpy as np
# Create a 1D array
arr_1d = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
# Using np.where() to find indices of elements greater than 5
indices_greater_than_5 = np.where(arr_1d > 5)
print("Indices of elements greater than 5 in 1D array:", indices_greater_than_5)
print("Elements greater than 5 in 1D array:", arr_1d[indices_greater_than_5])
# Using np.argmax() and np.argmin()
max_index = np.argmax(arr_1d)
min_index = np.argmin(arr_1d)
print("Index of the maximum value in 1D array:", max_index)
print("Index of the minimum value in 1D array:", min_index)
# Create a 2D array
arr_2d = np.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
# Using np.where() in 2D array to find indices of elements equal to 5
indices_equal_to_5 = np.where(arr_2d == 5)
print("Indices of elements equal to 5 in 2D array:")
print(indices_equal_to_5)
print("Elements equal to 5 in 2D array:", arr_2d[indices_equal_to_5])
np.where()
: np.where()
returns a tuple of arrays, one for each dimension. When indexing the original array, make sure to use these arrays correctly.np.argmax()
and np.argmin()
return the index of the first occurrence of the maximum or minimum value. If there are multiple such values, this might not be the desired behavior.Sorting and searching are essential operations when working with NumPy arrays. Sorting helps in arranging data in a meaningful order, while searching allows us to find specific elements or their positions. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, you can effectively use these operations in real - world applications such as data analysis, machine learning, and scientific computing.