numpy.min() in Python: Get Minimum Value in Array

Numpy provides us with many functions to perform different types of operations on Numpy arrays, one of them is the numpy.min() function. numpy.min() function is used to find the minimum value in arrays. In this article, we will look at three ways to find the minimum element from an array using this function. Let’s first look at numpy.min() function’s syntax, parameters and return value.

Related Article: numpy.max() in Python – Get Maximum Value in Array

Introducing numpy.min() Function

The numpy.min() function in Python’s NumPy library finds the minimum value in a NumPy array. It returns the minimum value among all the elements in the array.

Syntax:

numpy.min(a, axis=None, out=None, keepdims=False, initial=<no value>, where=True)

Parameters:

  • a: The input array from which we want to find the minimum value.
  • axis (optional): If specified, it allows us to find the minimum value along a particular axis of the input array, resulting in a reduced dimension array.
  • out (optional): An alternative output array in which to place the result.
  • keepdims (optional): If True, the dimensions of the output array will match those of the input array with reduced dimensions set to 1.
  • initial (optional): The initial value from which the minimum is determined.
  • where (optional): A boolean array that specifies where to compute the minimum.

Finding Minimum Element in NumPy Array

There are three ways through which we can find the minimum element from the array by using numpy.min() function:

  • Minimum element from the whole array.
  • Minimum element from array rows.
  • Minimum element from array columns.

1. Minimum Element from the Entire Array

Here by using the numpy.min() function we will find the minimum element from the entire array.

Example 1:

import numpy as np
arr1 = np.array([1,2,3,4])
print(np.min(arr1))

In the above code, to create a Numpy array and to use the min() function, we first imported the Numpy library as np then we created a 1D array of values 1,2,3,4 and saved it into a variable called arr1. Then, at last, we applied the numpy.min() function on arr1.

Output:

Example 1: Minimum element from the whole array

In the above output, we got the result as 1 which is the minimum value in the entire array.

Example 2:

import numpy as np
arr2 = np.array([[1,2,3],[4,5,6]])
print(np.min(arr2))

In the above code, we created a 2D array by using the NumPy library and saved it into a variable called arr2. Then we applied the numpy.min() function on it.

Output:

Example 2: Minimum element from the whole array

The output confirmed that the function works successfully on 2D arrays as well.

2. Minimum Element from Array Rows

Here we will provide axis=1 as a parameter in the numpy.min() function for getting the minimum element from the array rows.

Example:

import numpy as np
arr2 = np.array([[1,2,3],[4,5,6]])
print(np.min(arr2,axis=1))

In the above code, we have passed axis=1 as a parameter to get the minimum element row-wise.

Output:

Example: Minimum element from array rows

In the above output, we got [1,4] because the first row has 1,2,3 where 1 is minimum and the second row has 4,5,6 where 4 is minimum.

3. Minimum Element from Array Columns

Here we will provide axis=0 as a parameter in the numpy.min() function for getting the minimum element from the array columns.

Example:

import numpy as np
arr2 = np.array([[1,2,3],[4,5,6]])
print(np.min(arr2,axis=0))

In the above code, we have passed axis=0 as a parameter to get the minimum element column-wise.

Output:

Example: Minimum element from array columns

In the above output, we got [1,2,3] because the first column has 1,4 where 1 is minimum, the second column has 2,5 where 2 is minimum and the third column has 3,6 where 3 is minimum.

Summary

The numpy.min() function is a versatile tool for finding the minimum value in arrays, and it plays a crucial role in various scientific, engineering, statistical, and data analysis tasks across multiple domains. In this tutorial, we have discussed numpy.min() function provided by Python’s NumPy library and also explored three ways to find the minimum element in the array using numpy.min() function. After reading this tutorial, we hope you can easily find the minimum element from the array in Python.

Reference

https://stackoverflow.com/questions/14611703/min-function-in-numpy-array

Priyanshu Singh
Priyanshu Singh
Articles: 44