Python Numpy Minimum Function [9 Easy Code Examples]

Python Numpy Minimum Function explained with easy python code examples in jupyter notebook

In this tutorial, we’ll learn about the Python Numpy minimum function. The role of this Numpy function and its all parameters will be explained with the help of Python practical code examples along with easy explanations of the codes.

What is Python Numpy Minimum Function?

The usage of this function is that it examines the element-wise corresponding array items and returns the minimum of them. The input can be arrays or an array and a scalar value. A new array is returned by the minimum function and that array includes all the minimum items returned during the operation.

Python Numpy Minimum Function explained with easy python code examples in jupyter notebook

minimum function of python numpy library code examples

minimum function parameters of python numpy explained with python code practical examples


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

Detailed Explanation of minimum() Parameters

  • The parameters(x1, x2) can be arrays or scalar values. On this data, the minimum operation is performed. Make sure that the two arrays have the same shape or that the appropriate data should be used if broadcasting.
  • The parameter(/) is called a “positional-only” marker. Its role is to specify that the parameters that follow must be set using their names, not the positions of them. In the above syntax, the parameters(x1 and x2) are positional parameters, while the other parameters are called keywords parameters.
  • The optional parameter (out) is used to store the resulting array. If not set, then a new resulting array will be generated having the specified data of output.
  • The optional parameter(where) is used to allow on which elements should the minimum operation be performed. We can pass it True(Boolean) which shows that apply the operation on all the items of the input. We can also pass it an array of Boolean items. In that case, if the item is false then no minimum operation will be performed on the corresponding items while if True then the action will be performed.
  • The (casting) parameter is also optional and its role is to control the rules for type casting in operation. The ‘same_kind’ is the default value it takes and it shows that the resulting array will have the same data type as the input. We can also try it with ‘equiv’, ‘no’, ‘unsafe’, ‘safe’, etc.
  • The optional parameter(order) defines the memory layout of the resulting array. The value it takes as a default is ‘K‘. We can also pass ‘C‘(row-major) or ‘F‘(column-major).
  • The parameter(dtype) is optional and it’s used to give a specific datatype to the resulting array. If this parameter is not set, then the datatype is taken from the type of data of the inputs.
  • The (subok) is an optional parameter and is used to allow whether to use the instances of subclass in the output. If we pass it True(default value is True), then the instances of the subclass are preserved.

Practical use of Python numpy.minimum() Function (9 Examples)

Example 1: minimum() on 1D Arrays

import numpy as np

array1 = np.array([3, 8, 2, 5])
array2 = np.array([5, 4, 6, 1])

resul = np.minimum(array1, array2)


[3 4 2 1]

In this example code, we passed the two 1D arrays as arguments to the minimum function and the resulting arrays show only the minimum items from the corresponding arrays.

Example 2: 2D arrays with numpy.minimum()

ar1 = np.array([[1,6] ,[3,5]])
ar2 = np.array([[7,4], [1,6]])
result = np.minimum(ar1, ar2)


[[1 4]
 [1 5]]

In this code, we passed two 2D arrays to the minimum function. The output results in a returning array that has only the minimum items returned after the operation between the corresponding array items.

Example 3: Using Array and Scalar Values (Broadcasting)

ar = np.array([4,7,53,2])
scalarV = 5
result2 = np.minimum(ar, scalarV)


[4 5 5 2]

In this Python code example, we passed the Numpy array as the first argument and a scalar value as the second argument to the Numpy minimum function. The scalar value will be compared with each item of the array and the corresponding minimum value will be added to the resulting array as seen in the output.

Example 4: (where) Parameter

arrayA = np.array([5,5,2,86])
arrayB = np.array([4,7,2,65])

condition = np.array([True,False,True,True])

resArray = np.minimum(arrayA, arrayB, where=condition)


[ 4  7  2 65]

In this code, we created a Numpy array of Boolean items. Then we passed this array to this ‘where’ parameter of the minimum function.

For instance, the second corresponding item should have a result of 5 as 5 is the minimum between them but then 7 is returned, which means the operation is not performed here because the array item we passed to where a parameter has False at that index. In that case, the specific location item of the second argument is added to the resulting array which in our case is 7.

Example 5: Implementing (out) Parameter

arrX = np.array([4,7,3,5])
arrY = np.array([2,6,4,1])

output = np.empty(4, dtype=int)

np.minimum(arrX, arrY, out=output)


[2 6 3 1]

In this code, we created an empty array. Then we passed this array to the ‘out‘ parameter. What this will do is that the resulting array will be stored in this array. It can be very useful for memory efficiency as it avoids creating a new array.

Example 6: (casting) Parameter of Minimum Function

arr1 = np.array([5, 6, 8], dtype=np.int16)
arr2 = np.array([1, 7, 9], dtype=np.float32)

resultA = np.minimum(arr1, arr2, casting='unsafe')


[1. 6. 8.]

In this code, we set the first array to be of datatype int while the second one to be of type float. Also, we passed the ‘unsafe’ to the casting parameter of the minimum function. It means it’ll allow the datatypes to be changed without checking for potential loss of precision. The result shows that now all the items are in float(even the integers are also converted to float).

Example 7: Parameter (Order) of numpy.minimum Function

a1 = np.array([[3, 7], [1, 7]], order='C')
a2 = np.array([[4, 8], [4, 9]], order='F')

resF = np.minimum(a1, a2, order='F')
print('Order F')

resC = np.minimum(a1, a2, order='C')
print('Order C')


Order F
[[3 7]
 [1 7]]
Order C
[[3 7]
 [1 7]]

In this example Python code, we specified the ‘order’ parameter and passed it ‘C‘(row-major) or ‘F‘(column-major). We used two examples but the shape is the same for both of them so the result looks the same. But the first output has the order ‘F'(Fortran Style) while the second one has the order ‘C(C Style).

Example 8: Using Parameter (dtype)

a1D = np.array([5.2, 2.3, 9.1])
b1D = np.array([4.6, 3.1, 9.8])
res = np.minimum(a1D, b1D, dtype=int, casting='unsafe')


[4 2 9]

In this example code, we set the ‘dtype‘ parameter to int which means the resulting output will have items in integer format. Also, make sure to use the (casting=’unsafe’) if the operation raises some error.

Example 9: Using (subok) Parameter

class MyArray(np.ndarray):

arA = np.array([1, 3, 5])
arB = np.array([2, 4, 4])
result = np.minimum(arA.view(MyArray), arB, subok=True)


[1 3 4]

In the code Python example, we created a ‘MyArray‘ class which is a subclass of ‘numpy.ndarray‘. After doing that, we specified the ‘subok‘ parameter for the preservation of subclasses in the output. The result has the subclasses instances retained.


In conclusion, hope you now have a well-detailed understanding of how to easily make use of the Python Numpy minimum function. We’ve walked ourselves through well-explained practical Python code examples in which we implemented the parameters of the Numpy minimum() function.

Thank you so much for reading it.

Leave a Comment

Your email address will not be published. Required fields are marked *