Python Min



In this article we will discuss how to find the minimum or smallest value in a Numpy array and it’s indices using numpy.amin().

Python min and max are built-in functions in python which returns the smallest number and the largest number of the list respectively, as the output. Python min can also be used to find the smaller one in the comparison of two variables or lists. The min function returns the item with the lowest value, or the item with the lowest value in an iterable. If the values are strings, an alphabetically comparison is done. Python list method min returns the elements from the list with minimum value.

numpy.amin()

Python’s numpy module provides a function to get the minimum value from a Numpy array i.e.
Arguments :

  • a : numpy array from which it needs to find the minimum value.
  • axis : It’s optional and if not provided then it will flattened the passed numpy array and returns the min value in it.
    • If it’s provided then it will return for array of min values along the axis i.e.
    • If axis=0 then it returns an array containing min value for each columns.
    • If axis=1 then it returns an array containing min value for each row.
Python Min

Let’s look in detail,

Find minimum value & it’s index in a 1D Numpy Array:

Let’s create a 1D numpy array from a list i.e.
Find minimum value:

Now let’s use numpy.amin() to find the minimum value from this numpy array by passing just array as argument i.e.
Output:
It returns the minimum value from the passed numpy array i.e. 11

Find index of minimum value :

Get the array of indices of minimum value in numpy array using numpy.where() i.e.
Output:
In numpy.where() when we pass the condition expression only then it returns a tuple of arrays (one for each axis) containing the indices of element that satisfies the given condition. As our numpy array has one axis only therefore returned tuple contained one array of indices.

Find minimum value & it’s index in a 2D Numpy Array

Let’s create a 2D numpy array i.e.
Contents of the 2D numpy array are,
Find min value in complete 2D numpy array

To find minimum value from complete 2D numpy array we will not pass axis in numpy.amin() i.e.
It will return the minimum value from complete 2D numpy arrays i.e. in all rows and columns.
Find min values along the axis in 2D numpy array | min in rows or columns:

If we pass axis=0 in numpy.amin() then it returns an array containing min value for each column i.e.
Output:
If we pass axis = 1 in numpy.amin() then it returns an array containing min value for each row i.e.
Output:

Find index of minimum value from 2D numpy array:

Min

Contents of the 2D numpy array arr2D are,
Let’s get the array of indices of minimum value in 2D numpy array i.e.
Output:

numpy.amin() & NaN

Python Min Heap

numpy.amin() propagates the NaN values i.e. if there is a NaN in the given numpy array then numpy.amin() will return NaN as minimum value. For example,
Output:
If you want to ignore the NaNs while finding the min values from numpy then use numpy.nanmin() instead.

Complete example is as follows,
Output

Related Posts:

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Updated on Jan 07, 2020

Python Minecraft

Build

The min() function returns the smallest of the input values.

Min

Its syntax is as follows:

PARAMETER DESCRIPTION
iterable (required)An iterable object like string, list, tuple etc.
default (optional)The default value to return if the iterable is empty.
key (optional)It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable.

or

PARAMETER DESCRIPTION
a, b, c ...Items to compare
key (optional)It refers to the single argument function to customize the sort order. The function is applied on each of the items.

If min() is called with an iterable, it returns the smallest item in it. If the iterable is empty then the default value is returned (assuming it is provided), otherwise a ValueError exception is raised.

If min() is called with multiple arguments, it returns the smallest one.

Let's see some examples:

Example 1: Calling min() with an iterable

Try it out:

Example 2: Calling min() with multiple arguments

Python Min

Try it out:

Trying to find largest value among the objects of different types causes an error.

Customing Sort Order #

To customize the sort order we use the key named argument. It works exactly like the key named argument of the sorted() function.

Python Min Max

Here is an example where we use key argument to make the string comparision case-insentive.

Try it out:

Min Value Of A List Python

The following is another example where we compare strings based on its length instead of their ASCII values.

Try it out:

There also exists a complementary function called max() which finds the largest of the input values.

Other Tutorials (Sponsors)

Python Minim

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Please enable JavaScript to view the comments powered by Disqus.