Python NumPy Minimum tutorial

In this Python tutorial, we will learn how to find Minimum elements in a NumPy array in Python. Also, we will cover these topics.

  • Python numpy minimum of array
  • Python numpy minimum index
  • Python numpy minimum float
  • Python numpy minimum of two values
  • Python numpy minimum of function
  • Python numpy minimum element
  • Python numpy minimum column
  • Python np.where min
  • Python numpy element wise min
  • Python Numpy least square example
  • Python numpy minimum of two arrays
  • Python numpy min and max

Python NumPy minimum

  • In this section, we will discuss how to get the element-wise minimum of arrays by using NumPy Python.
  • To perform this particular task we are going to use the numpy.minimum() method. In Python the numpy.minimum() method is used for comparing two numpy arrays in Python and always returns a new numpy array that stores the element-wise minima.
  • This method will check the condition if one of the values being compared is a Nan(Not a number) then that item always returns in the result. while in the case if both of the values are nan then the first one is returned.

Syntax:

Here is the Syntax of numpy.minimum() method

numpy.minimum
             (
              x1,
              x2,
              /,
              out=None,
              *,
              where=True,
              casting='same_kind',
              order='K',
              dtype=None,
              subok=True
              [,
              signature,
              extobj
              ]
             )
  • It consists of a few parameters:
    • x1,x2: This parameter indicates which elements to be compared and it must be the same shape.
    • out: By default, it takes the ‘None’ value and if the none value is not provided then it will return a freshly allocated array.
    • where: By default, the condition is True that indicates the output array and will be set to ufunc result.
  • Let’s take an example and understand the working of numpy.minimum() method
import numpy as np

new_arr = np.array([45,12,13])
new_arr2 = np.array([53,13,15])
final_result=np.minimum(new_arr,new_arr2)
print("Minimum array element:",final_result)

In the above code first, we imported a NumPy library and then use the np.array() for creating an array. Now declare a variable ‘final_result’ and assign a function numpy.minimum().

Once you will print ‘final_result’ then the output will display the new array that contains only minimum values.

Here is the Screenshot of the following given code

Python NumPy minimum
Python NumPy minimum

Read Python Numpy Not Found – How to Fix

Python numpy minimum of array

  • Here we can see how to find the minimum value in a NumPy array by using Python.
  • To do this task we can easily use the function np.min(). In Python, this function is used to find the minimum value in an array along with a specified axis. This method is always available in the NumPy module.
  • In Python, this method returns the minimum value of a ndarray object and is also based on the given axis.

Syntax:

Here is the Syntax of numpy.min() method

numpy.min
         (
          input_array,
          axis=None,
          out=None,
          keepdims=None,
          initial=None,
          where=None
         )         
  • It consists of a few parameters
    • input_array: This parameter indicates which arrays we want to use in numpy.min() function.
    • axis: This is an optional parameter and by default, it takes no value and it is also used for the multi-dimensional arrays.
    • keepdims: This argument represents the boolean value.

Example:

Let’s take an example and check how to find the minimum value in an array by using NumPy Python

Source Code:

import numpy as np

new_val= np.arange(20)
result= np.min(new_val)
print("Minimum value:",result)

In the above code, we imported a NumPy library and then use the np.arange() method. Now use the np.min() method and within this function pass the ‘new_val’ array as an argument. Once you will print ‘result’ then the output will display the minimum value from a given array.

Here is the implementation of the following given code

Python numpy minimum of array
Python numpy minimum of an array

As you can see in the Screenshot the output displays the ‘0’ minimum value

Read Python NumPy Delete

Python numpy minimum index

  • In this Program, we will discuss how to find the index of minimum value by using NumPy Python.
  • To perform this particular task we are going to apply the method numpy.argmin(). In Python, the numpy.argmin() is used for getting the index number of a minimum value from a numpy array along with a specified axis.
  • This method takes an array as input and always returns the index of the minimum value and the array can be 2-dimensional, one-dimensional or we can say multi-dimensional array for performing this task.
  • Suppose we have an array that contains the integer values and every location in that array has an index. To find the index of minimum value we have to use the numpy.argmin() function.

Syntax:

Here is the Syntax of numpy.argmin() method

numpy.argmin
            (
             a,
             axis=None,
             out=None
            )
  • It consists of a few parameter
    • a: This parameter indicates the array you want to operate and found the index number.
    • axis: It is an optional parameter and by default, it takes none value and it will check the condition if the axis=1 then it will find the index row-wise otherwise column-wise.
    • out: If it is specified then the output of the np.argmin() function will be stored into this array and it should be of proper shape and size.

Example:

Let’s take an example and understand the working of numpy.argmin() function

Source Code:

import numpy as np

new_array= np.arange(2,6)
print("Creation of array:",new_array)
result= np.argmin(new_array)
print("Index number of minimum valuwe:",result)
  • In the above code, we have imported the numpy library and initialized a variable, and stored the array into it.
  • Now use the numpy.argmin() function and pass the array ‘new_array’ as an argument. Once you will print ‘result’ then the output displays the index number of the minimum number.

Here is the Screenshot of the following given code

Python numpy minimum index
Python numpy minimum index

Read Python Numpy Factorial

Python numpy minimum float

  • In this section we will discuss how to find the minimum float value in NumPy array by using Python.
  • In this example we are going to use the numpy.min() function for getting the minimum decimal values from an array. In Python the np.min() is used to find the minimum value in an array along with specified axis.

Syntax:

Here is the Syntax of numpy.min() function

numpy.min
         (
          input_array,
          axis=None,
          out=None,
          keepdims=None,
          initial=None,
          where=None
         ) 

Example:

import numpy as np

new_val= np.array([14.5,11.2,8.7,22.3,45.6])
new_output= np.min(new_val)
print("Minimum floating value:",new_output)
  • In this example, we are going to use the numpy.min() function for getting the minimum decimal values from an array. In Python, the np.min() is used to find the minimum value in an array along with a specified axis.
  • Once you will print ‘new_output’ then the result will display a minimum float value from a given array.

Here is the Output of the following given code

Python numpy minimum float
Python numpy minimum float

Read Python NumPy Stack with Examples

Python numpy minimum of two values

  • In this section, we will discuss how to find a minimum number between two values by using NumPy Python.
  • To do this task we can easily use the concept of numpy.minimum() function along with np.cos() function. In Python, the numpy.minimum() is used to get the element-wise minimum of array values and always return a new numpy array that stores the element-wise minima.
  • In Python, the Numpy.cos() function is used to calculate the trigonometric cosine for all values. In this example, we are going to apply this function for getting the minimum value between two values.

Syntax:

Here is the Syntax of numpy.cos() function

numpy.cos
         (
          x,
          /,
          out=None,
          *,
          where=True,
          casting='same_kind',
          order='K',
          dtype=None,
         )

Example:

Let’s take an example and check how to find the minimum number between two values

Source Code:

import numpy as np

new_arr = np.array([16,18,20,22,24])

result = np.minimum(np.cos(2*new_arr*np.pi), 4/2)
print("Minimum of two values:",result)

Here is the execution of the following given code

Python numpy minimum of two values
Python numpy minimum of two values

Read Python NumPy round

Python numpy minimum of function

  • In this section, we will discuss how to use the np.amin() function in NumPy Python.
  • In Python this method is used to find the minimum value from the n-dimensional array and this is a statistical function and always available in the numpy module and returns the minimum array.
  • If we are using two or three-dimensional arrays in our program then, we have to find the smallest value of each row as well as column. This function takes four arguments along with the axis parameter.

Syntax:

Here is the Syntax of numpy.amin() function

numpy.amin
          (
           a,
           axis=None,
           out=None,
           keepdims=<no value>,
           initial=<no value>,
           where=<no value>
          )
  • It consists of a few parameters
    • a: This parameter specifies the input data from which we can easily find the min value.
    • axis: By default, it sets the value ‘None’ and it will check the condition if the axis=0 then it will find the index column-wise otherwise row-wise.
    • out: This is an optional parameter and specifies the output array in which the output is to be placed.

Example:

Let’s take an example and understand the working of numpy.amin() function

Source Code:

import numpy as np

new_val= np.arange(16).reshape((4,4))
print("Creation of array:",new_val)
output= np.amin(new_val)
print("Minimum value:",output)

In the above code we imported the numpy library and then initialized an array by using the np.arange() function along with np.reshape().

Once you will print ‘new_val’ then the output will display the new array. Now use the np.amin() and pass an array as an argument.

You can refer to the below Screenshot

Python numpy minimum of function
Python numpy minimum of a function

As you can see in the Screenshot the output displays the minimum value ‘0’

Read Python Numpy unique

Python numpy minimum element

  • In this Program, we will discuss how to find the minimum element in the NumPy array by using Python.
  • In this example we ae going to use the np.min() function and get the minimum element from a given array.

Syntax:

Here is the Syntax of np.min() method

numpy.min
         (
          input_array,
          axis=None,
          out=None,
          keepdims=None,
          initial=None,
          where=None
         )    

Source Code:

import numpy as np

values= np.array([23,14,7,8,34])
new_result= np.min(values)
print("Minimum value:",new_result)

Here is the execution of the following given code

Python numpy minimum element
Python numpy minimum element

You can see in the Screenshot the output displays the minimum value ‘7’

Read Python NumPy repeat

Python numpy minimum column

  • In this section, we will discuss how to find the minimum value of each column in the NumPy array by using Python.
  • To perform this particular task we are going to use the numpy.minimum() function along with axis=0 that indicates the column-wise value.

Example:

import numpy as np

a = np.array([[12,5], [1,1], [16,8]])
b= np.min(a,axis=0)
print("Minimum values from column:",b)
  • In the above code, we imported the numpy library and then use the np.array() function for creating an array.
  • Now use the np.min() function and within this method assign axis=0. Once you will print ‘b’ then the output will display the minimum value of each column.

Here is the implementation of the following given code

Python numpy minimum column
Python numpy minimum column

Also read, Python NumPy Data types

Python np.where min

  • In this section, we will discuss how to use the np.where() function in NumPy Python
  • In this example, we are going to use the numpy.array() function for creating an array and then use the np.min() function and it will get the minimum number after that we are going to apply the np.where() function.

Syntax:

Here is the Syntax of numpy.where() method

numpy.where
           (
            condition
            [,
            x,
            y
            ]
           )

Example:

import numpy as np

new_array=np.array([15,56 ,4,  9, 65])
m = np.min(new_array)
u = np.where(new_array == m)
z=new_array[u]
print(z)

Here is the Output of the following given code

Python np where min
Python np where min

Read Python NumPy 2d array

Python numpy element wise min

  • In this Program, we will discuss how to get the minimum element in NumPy array by using Python.
  • In this example, we are going to use the np.min() function and pass the array as an argument. Once you will print ‘new_output’ then the result will display the minimum value.

Example:

import numpy as np

array= np.array([13,10,45,87,67,19])
new_output= np.min(array)
print("Minimum value element-wise:",new_output)

Here is the Screenshot of the following given code

Python numpy element wise min
Python numpy element-wise min

Read Python NumPy 3d array + Examples

Python Numpy least square example

  • In this section, we will discuss how to get the least square in the NumPy array by using Python.
  • To perform this particular task we are going to use the numpy.linalg.lstsq() method. In Python, this method is used to get the least-square to a matrix equation ax=b.
  • This function is available in the NumPy module and this method takes the matrices as an argument and always returns the solution. It will check the condition if it is a 2-dimensional array then the result in the K column of x.

Syntax:

Here is the Syntax of numpy.linalg() function

numpy.linalg.lstsq
                  (
                   a,
                   b,
                   rcond='warn'
                  )
  • It consists of a few parameters
    • a,b: This parameter indicates the matrices and it will check the condition if it is a 2-dimensional array in an argument then the least is measured for each of the K columns.
    • rcond: By default, the value takes the ‘warn’ value and it should be a singular value and treated as zero.

Source Code:

import numpy as np

arr1=[[5,5,5,5],[5,5,5,5],[5,5,5,5],[5,5,5,5],[5,5,4,4]]
arr2=[5,5,5,5,5]
new_result=np.linalg.lstsq(arr1, arr2, rcond = -1)
print("Least square number:",new_result[0])

In the above code, we imported the numpy library and then use the matrix equation ax=b along with np.linalg.lstsq() method within this function we passed the arrays in it. Once you will print ‘new_result[0]’ then the output will display the least square solution.

Here is the Screenshot of the following given code

Python Numpy least square example
Python Numpy least-square example

Read Python NumPy append

Python numpy minimum of two arrays

  • In this Program, we will discuss how to get the minimum element between two arrays by using NumPy Python.
  • By using the numpy.min() function we can easily get the minimum value from two given arrays. To do this task first we have to import the numpy module and then initialize an array by using the np.array() function.
  • Now declare a variable ‘final_output’ and assign a function min as an argument and within this function, we have set the array ‘new_arr1’ and ‘new_arr2’.

Source Code:

import numpy as np

new_arr1=np.array([23,45,67])
new_arr2=np.array([64,12,6])
final_output = min(min(new_arr1),min(new_arr2))
print("Minimum value of two arrays:",final_output)

Here is the implementation of the following given code

Python numpy minimum of two arrays
Python numpy minimum of two arrays

As you can see in the Screenshot the output displays a minimum value from two arrays

Read Python NumPy arange

How to check minimum value of two arrays in Python

By using the np.minimum() function we can solve this problem. In Python, the numpy.minimum() is used for comparing two numpy arrays and always returns a new numpy array.

Syntax:

Here is the Syntax of numpy.minimum() function

numpy.minimum
             (
              x1,
              x2,
              /,
              out=None,
              *,
              where=True,
              casting='same_kind',
              order='K',
              dtype=None,
              subok=True
              [,
              signature,
              extobj
              ]
             )

Source Code:

import numpy as np

new_values1= np.array([45,21,29])
new_values2= np.array([85,32,12])
result = np.minimum(new_values1,new_values2)
print("Minimum value:",result)

In the above code, we imported the numpy library and then use the np.minimum() function in which we assigned the arrays ‘new_values1’ and ‘new_values’ as an argument.

Once you will print ‘result’ then the output will display the minimum value from two arrays and store them into a new array.

Here is the Screenshot of the following given code

Python numpy minimum of two arrays
Python numpy minimum of two arrays

Read Python NumPy Sum

Python numpy min and max

  • In this section, we will discuss how to find the maximum and minimum value in NumPy Python.
  • In Python the max() function is used to find a maximum value along with a given axis and this function contains only numeric values in an array and within this function, there are several arguments like axis, arr, keepdims.
  • In Python, the min() function is used to find a minimum value along with a specified axis.

Example:

Let’s take an example and check how to find the maximum and minimum value in NumPy Python.

Source Code:

import numpy as np

new_array=np.array([23,4,6,16,72,95,44])
z= np.max(new_array)
print("Maximum value:",z)
d= np.min(new_array)
print("Minimum value:",d)

Here is the execution of the following given code

Python numpy min and max
Python numpy min and max

Related Python NumPy tutorials:

In this Python tutorial, we learned how to find Minimum elements in a NumPy array by using Python. Also, we will cover these topics.

  • Python numpy minimum of array
  • Python numpy minimum index
  • Python numpy minimum float
  • Python numpy minimum of two values
  • Python numpy minimum of function
  • Python numpy minimum element
  • Python numpy minimum column
  • Python np.where min
  • Python numpy element wise min
  • Python Numpy least square example
  • Python numpy minimum of two arrays
  • Python numpy min and max