numpy.min
The numpy.min
function is used to return the minimum value of an array or along a specified axis. If no axis is specified, it returns the minimum value of the entire array.
To find the index of the minimum value in a NumPy array, we can use the numpy.argmin
function. This function returns the indices of the minimum values along an axis. If no axis is provided, it flattens the array and returns the index of the minimum value in the flattened array.
import numpy as np
# Create a 1-D array
arr = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
# Find the minimum value
min_value = np.min(arr)
# Find the index of the minimum value
min_index = np.argmin(arr)
print(f"Minimum value: {min_value}")
print(f"Index of the minimum value: {min_index}")
In this example, np.min(arr)
returns the smallest value in the array, and np.argmin(arr)
returns the index of the first occurrence of the minimum value.
import numpy as np
# Create a 2-D array
arr_2d = np.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])
# Find the index of the minimum value along each row (axis = 1)
min_indices_row = np.argmin(arr_2d, axis = 1)
print("Indices of minimum values along each row:")
print(min_indices_row)
Here, axis = 1
indicates that we want to find the minimum value index for each row in the 2 - D array.
When there are multiple occurrences of the minimum value in an array, np.argmin
returns the index of the first occurrence. If you want to find all the indices of the minimum values, you can use the following approach:
import numpy as np
arr = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
min_value = np.min(arr)
all_min_indices = np.where(arr == min_value)[0]
print("All indices of the minimum value:")
print(all_min_indices)
In this code, np.where(arr == min_value)
returns the indices where the array elements are equal to the minimum value.
NumPy’s masked arrays can be used to ignore certain elements while finding the minimum index.
import numpy as np
import numpy.ma as ma
arr = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
mask = [False, True, False, False, False, False, False, False, False, False, False]
masked_arr = ma.masked_array(arr, mask = mask)
min_index = np.argmin(masked_arr)
print("Index of the minimum value in the masked array:")
print(min_index)
Here, the element at the index where the mask is True
is ignored while finding the minimum index.
np.argmin
instead of first calculating np.min
and then finding its index.np.int8
or np.uint8
instead of the default np.int64
.np.min
or np.argmin
, check if the array is empty. If the array is empty, these functions may raise an error.import numpy as np
arr = np.array([])
if arr.size > 0:
min_index = np.argmin(arr)
print("Index of the minimum value:", min_index)
else:
print("The array is empty.")
In this blog post, we have explored the fundamental concepts of finding the minimum index in NumPy. We have learned about the numpy.min
and numpy.argmin
functions, different usage methods, common practices such as handling multiple minimum values and using masked arrays, and best practices for performance and error handling. By following these guidelines, you can efficiently use these functions in your NumPy - based projects.