{"id":5468,"date":"2023-10-28T12:06:28","date_gmt":"2023-10-28T06:36:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=5468"},"modified":"2023-11-10T12:04:51","modified_gmt":"2023-11-10T06:34:51","slug":"numpy-amin-find-minimum-value-in-numpy-array-and-its-index","status":"publish","type":"post","link":"https:\/\/python-programs.com\/numpy-amin-find-minimum-value-in-numpy-array-and-its-index\/","title":{"rendered":"numpy.amin() | Find minimum value in Numpy Array and it\u2019s index | Python Numpy amin() Function"},"content":{"rendered":"
In this tutorial, we have shared the numpy.amin() statistical function of the Numpy library with its syntax, parameters, and returned values along with a few code examples to aid you in understanding how this function works. Also, you can easily find the minimum value in Numpy Array and its index using Numpy.amin() with sample programs.<\/p>\n
The numpy.amin() function returns minimum value of an array. Also, it is a statistical function of the NumPy library that is utilized to return the minimum element of an array or minimum element along an axis.<\/p>\n
The syntax needed to use this function is as follows:<\/p>\n
In this, we will pass two arguments-<\/p>\n where<\/p>\n Now it’s time to explain the parameters of this method:<\/p>\n The minimum of an array \u2013 arr[ndarray or scalar], scalar if the axis is None; the result is an array of dimension a.ndim \u2013 1 if the axis is mentioned.<\/p>\n Also Refer:<\/span><\/p>\n Output:<\/strong><\/p>\n So now we are going to use numpy.amin() to find out the minimum element from a 1D array.<\/p>\n Output:<\/strong><\/p>\n So here we are going to find out the min value in the 2D array.<\/p>\n Output:<\/strong><\/p>\n If we pass axis=0 then it gives an array containing min of every column,<\/p>\n Output:<\/strong><\/p>\n If we pass axis=1 then it gives an array containing min of every row,<\/p>\n Output:<\/p>\n So here we are going to discuss how to find out the axis and coordinate of the min value in the array.<\/p>\n Output:<\/strong><\/p>\n if there is a NaN in the given numpy array then numpy.amin() will return NaN as minimum value.<\/p>\n Output:<\/strong><\/p>\n In this tutorial, we have shared the numpy.amin() statistical function of the Numpy library with its syntax, parameters, and returned values along with a few code examples to aid you in understanding how this function works. Also, you can easily find the minimum value in Numpy Array and its index using Numpy.amin() with sample programs. …<\/p>\nnumpy.amin(a, axis=None, out=None, keepdims=<no value>, initial=<no value>)<\/code><\/p>\n
\n
\n
<\/a>Parameters:<\/h3>\n
\n
<\/a>Return Value:<\/h3>\n
\n
<\/a>Example on NumPy amin() Function<\/h2>\n
a = np.arange(9).reshape((3,3))\r\n\r\nprint(\"The Array is :\")\r\nprint(a)\r\n\r\nprint(\"Minimum element in the array is:\",np.amin(a)) \r\n\r\nprint(\"Minimum element along the first axis of array is:\",np.amin(a, axis=0)) \r\n\r\nprint(\"Minimum element along the second axis of array is:\",np.amin(a, axis=1))<\/pre>\n
The Array is : [[0 1 2] [3 4 5] [6 7 8]]\r\n\r\nMinimum element in the array is: 0\r\n\r\nMinimum element along the first axis of array is: [0 1 2]\r\n\r\nMinimum element along the second axis of array is: [0 3 6]<\/pre>\n
<\/a>Find the minimum value in a 1D Numpy Array<\/h2>\n
import numpy\r\narr = numpy.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])\r\n# Get the minimum element from a Numpy array\r\nminElement = numpy.amin(arr)\r\nprint('Minimum element from Numpy Array : ', minElement)\r\n<\/pre>\n
Minimum element from Numpy Array : 11\r\n\r\n<\/pre>\n
<\/a>Find minimum value & its index in a 2D Numpy Array<\/h2>\n
import numpy\r\narr2D = numpy.array([[11, 12, 13],\r\n [14, 15, 16],\r\n [17, 15, 11],\r\n [12, 14, 15]])# Get the minimum element from a Numpy array\r\nminElement = numpy.amin(arr2D)\r\nprint('Minimum element from Numpy Array : ', minElement)\r\n\r\n<\/pre>\n
Minimum element from Numpy Array : 11\r\n\r\n<\/pre>\n
<\/a>Find min values along the axis in 2D numpy array | min in rows or columns:<\/h2>\n
import numpy\r\narr2D = numpy.array([[11, 12, 13],\r\n [14, 15, 16],\r\n [17, 15, 11],\r\n [12, 14, 15]])\r\n# Get the minimum values of each column i.e. along axis 0\r\nminInColumns = numpy.amin(arr2D, axis=0)\r\nprint('min value of every column: ', minInColumns)\r\n\r\n<\/pre>\n
min value of every column: [11 12 11]\r\n\r\n<\/pre>\n
import numpy \r\narr2D = numpy.array([[11, 12, 13],\r\n [14, 15, 16], \r\n [17, 15, 11], \r\n [12, 14, 15]]) \r\n# Get the minimum values of each row i.e. along axis 1 \r\nminInColumns = numpy.amin(arr2D, axis=1) \r\nprint('min value of every column: ', minInColumns)<\/pre>\n
min value of every column: [11 14 11 12]\r\n<\/pre>\n
<\/a>Find the index of minimum value from the 2D numpy array<\/h2>\n
import numpy\r\narr2D = numpy.array([[11, 12, 13],\r\n [14, 15, 16],\r\n [17, 15, 11],\r\n [12, 14, 15]])\r\nresult = numpy.where(arr2D == numpy.amin(arr2D))\r\nprint('Tuple of arrays returned : ', result)\r\nprint('List of coordinates of minimum value in Numpy array : ')\r\n# zip the 2 arrays to get the exact coordinates\r\nlistOfCordinates = list(zip(result[0], result[1]))\r\n# travese over the list of cordinates\r\nfor cord in listOfCordinates:\r\n print(cord)\r\n<\/pre>\n
Tuple of arrays returned : (array([0, 2], dtype=int32), array([0, 2], dtype=int32))\r\nList of coordinates of minimum value in Numpy array :\r\n(0, 0)\r\n(2, 2)\r\n<\/pre>\n
<\/a>numpy.amin() & NaN<\/h2>\n
import numpy\r\narr = numpy.array([11, 12, 13, 14, 15], dtype=float)\r\narr[3] = numpy.NaN\r\nprint('min element from Numpy Array : ', numpy.amin(arr))<\/pre>\n
min element from Numpy Array : nan\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"