Let us discuss.
NumPy Argmin Function Syntax
The function provides a minimalistic syntax as shown:
Parameters
The function parameters are as shown below:
- a – refers to the input array. This is a non-optional parameter.
- Axis – specifies along which axis to apply the argmin() function. If set to None, the function will flatten the array and use the function on all elements.
- Out – specifies an alternative output array. The output array must have the same shape as the output value.
- Keepdims – a Boolean value that allows you to preserve the axes reduced in the result as dimensions with a size of one.
Function Result
The function will return an array of indices with the same shape as a.shape and the dimensions along the specified axis removed.
Example 1
The following is an example that illustrates how to use the argmin() function with a 1D array in Python.
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8])
print(f"index of min element -> {np.argmin(arr)}")
In the code above, we have a 1D array holding elements from 1 to 8. We then check the minimum element in the array using the argmin() function and return its index.
The output is as shown:
Example 2
Let us see what happens when applying the same operation on a 2D array.
print(f"index of min element -> {np.argmin(arr_2d)}")
In the code above, we apply the argmin() function to a 2D array without specifying the axis. This flattens the array and applies the function.
The resulting value is as shown:
Example 3
To act along a specific axis, we can set the axis parameter as shown:
print(f"indices of min elements -> {np.argmin(arr_2d, axis=0)}")
The code above should apply the argmin() function along axis 0 and return the indices of the min elements as shown in the output array:
[0 0 0 0]]
Example 4
To apply the function on the last axis, we can set the axis value as -1 as shown below:
print(f"indices of min elements -> {np.argmin(arr_2d, axis=-1)}")
The code above should return:
Conclusion
Throughout this article, we explored the NumPy argmin function, its syntax, parameters, and return values. We also provided various examples illustrating how the function works in multiple scenarios.
Happy coding!!
from https://ift.tt/fwBJLGM
0 Comments