Maximum Function of Python Numpy [8 Code Examples]

Maximum Function of Python Numpy explained with code examples parameters explained in jupyter notebook

In this article, the maximum function of Python Numpy will be explained practically. The parameters of the maximum() function will be explained with the help of Python code examples so you can have a proper understanding of how and when to use them. All the examples will be explained step by step.

What is the Maximum Function of Python Numpy?

The role of this function is to compare element-wise two arrays or an array and a scalar value and return a new array that contains the maximum value in each corresponding position.

Maximum Function of Python Numpy explained with code examples parameters explained in jupyter notebook

Maximum Function of Python Numpy library code examples

python numpy maximun function code examples parameters

 

Syntax

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

Parameter’s Explanation

  1. The ‘x1 and x2’ specifies two arrays or they can be scalar values as well. Their corresponding values will be compared and the maximum will be put in the new array.
  2. The optional parameter ‘out‘ specified the array in which the output will be stored. A new array will be created to store the result if this parameter is not used.
  3. By making the use of optional parameter ‘where‘, we can set which items we want to modify. If it’s not specified, then all the items will be gone through the process.
  4. The kind of data casting while the operation is performing is controlled using the optional parameter ‘casting‘. It takes values like ‘no’, ‘same_kind’, ‘equiv’, ‘safe’, or  ‘unsafe’.
  5. The parameter optional ‘order‘ is used to set the memory layout for the resulting array. The values it takes can be ‘C'(row-major), ‘F'(column-major), or ‘A’ (any, the default).
  6. The ‘dtype‘ parameter is also optional and its role is to give a specified datatype to the resulting array. If it’s not set, then the datatype will be taken from the items of the input array.
  7. If we set the optional parameter ‘subok‘ to true, then the resulting array will be a subclass of the class of the input’s array. The default is false which means the output array will be a base class array.
  8. The optional parameter ‘signature‘ is used for the purpose of checking type in ‘numpy.dispatch mechanism.
  9. The parameter ‘extopj‘ is optional and it’s used to allow the passage of extra data to low-level routines which specifies that it’s used for internal purposes.

Practical Implementation of maximum() Function (8 Examples)

Example 1: Maximum Function Applied on two 1D Arrays

import numpy as np

arra1 = np.array([1, 5, 8, 3])
arra2 = np.array([2, 6, 3, 7])

result = np.maximum(arra1, arra2)
print(result)

Output

[2 6 8 7]

In this code, we have passed the two 1D(one-dimensional) arrays to the maximum function. The output shows that all the maximum corresponding values from these 2 arrays have been added to the new resulting array.

Example 2: Using an Array and a Scalar Value

ar = np.array([10, 20, 30, 40])
sc = 25

res = np.maximum(ar, sc)
print(res)

Output

[25 25 30 40]

In this code, we passed an array and a scalar value as arguments to the maximum function. The result shows that the scalar value is compared with all the values of the array and only the maximum corresponding values were stored inside the output array.

Example 3: Applied numpy.maximum() on 2D Arrays

ar1=np.array([[1,4,5],[2,8,3]])
ar2=np.array([[6,7,2],[52,8,4]])

resAr= np.maximum(ar1,ar2)
print(resAr)

Output

[[ 6  7  5]
 [52  8  4]]

In this code, 2D arrays were passed to maximum function and the resulting array has all the corresponding maximum items from these two arrays. Also, we can see that when the value of two corresponding items matches then the same value is added to the result as well like ‘8‘ in the above output.

Example 4: Parameter ‘where’

arr1 = np.array([9,1, 3, 7, 9])
arr2 = np.array([1,2, 5, 1, 8])

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

rArr = np.maximum(arr1, arr2, where=c)
print(rArr)

Output

[9 2 5 7 8]

In this code, we created an array of Boolean items. Also, we passed this array to the ‘where‘ parameter of the maximum function. We can see that when the false item of the array is executed, then no maximum comparison action is performed. For instance, the last item of the resulting array should be 9 but it’s 8, reason is that the operation performing Boolean value is false in the last. There are other examples in the same code as well.

Example 5: ‘casting’ Parameter of maximum() Function

ar1 = np.array([6, 8, 5], dtype=np.int16)
ar2 = np.array([4, 5, 6], dtype=np.float32)

resultAr = np.maximum(ar1, ar2, casting='safe')
print(resultAr)

Output

[6. 8. 6.]

In this Python code example:

We first created two Numpy arrays and assigned different datatypes to them.

Then we passed them as arguments of maximum function. Also, we specified the ‘casting‘ parameter to ‘safe‘.

The output array shows items having a data type of float. Give it a try by passing other values like ‘equiv’, ‘safe’,  ‘unsafe’, ‘no’, or ‘same_kind’,  to the parameter ‘casting‘ and see what output is returned.

Example 6: Making use of the ‘dtype’ Parameter

array1 = np.array([1.3, 5.7, 8.9])
array2 = np.array([3.1, 4.8, 9.3])

resultArray = np.maximum(array1, array2, dtype=np.int32, casting='unsafe')
print(resultArray)

Output

[3 5 9]

In this code, we specified the ‘dtype’ parameter to np.int32. Also, we made the casting ‘unsafe’, the reason is that if you don’t use it then a UFuncTypeError will occur. The output shows that all the items are now in integer format.

Example 7: ‘order’ Parameter Implementation

arr1 = np.array([[2, 4], [4, 8]], order='C') 
arr2 = np.array([[5, 2], [3, 9]], order='F')

resultF = np.maximum(arr1, arr2, order='F')
print(resultF)

resultC = np.maximum(arr1, arr2, order='C')
print(resultC)

Output

[[5 4]
 [4 9]]       # order F
[[5 4]        # order C
 [4 9]]

In this code, we specified the ‘order’ parameter and passed it ‘C‘ and ‘F‘. We stored the resulting array of order ‘C‘ in one variable and the order ‘F‘ in another variable and prints it. The first array in the output has the order ‘F‘ while the second array specifies the order ‘C‘.

Example 8: Parameter ‘subok’

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

result = np.maximum(array1, array2, subok=True)

print(result)
print(type(result))

Output

[4 6 8]
<class 'numpy.ndarray'>

In this example, we passed the Boolean value True to the ‘subok’ parameter which specifies that if possible, then the output array will be subclassed from the input arrays. In some cases, the default behavior works just fine but it’s used in cases where you want the resulting array to have the same subclassing as the input arrays.

Conclusion

In conclusion, we practically learned the maximum function of Python Numpy with 8 easy code examples and with proper explanations. Do write to us if you still have any questions related to this function.

Thank you for reading this article.

Leave a Comment

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