How to Use Numpy Maximum on Arrays?

Numpy Maximum

Bigger things matter! Those are the entities that sow the seeds for biasing the results of the datasets, skewing their averages and giving the audience an illusion that is not even close to what exists in the reality. In this article, we will look into the sequence of steps involved in finding the maximum out of the given elements in the arrays using the maximum( ) function from the numpy library in Python. Before kicking things off, let us start with understanding its syntax.

Also read: How to Use Numpy Minimum on Arrays?


Syntax of maximum( ) function

Following are the basic constructs comprising both mandatory and optional elements that are to be provided for the effective functioning of the maximum( ) function from the numpy library.

numpy.maximum(x1, x2, out=None, *, where=True, dtype=None)

where,

  • x1, x2 – input arrays holding the elements for which the maximum is to be found
  • out – an optional construct set to none by default, but could be used to store the results in the desired array which is of the same length as the output
  • *kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function
  • where –an optional construct which is used to calculate the universal function (ufunc) at the given position when set to True (default setting) or not calculate when set to False
  • dtype – an optional construct used to specify the data type which is being used

Using maximum( ) on One Dimensional Arrays

Let us kick things off by importing the numpy library within Python by using the following code.

import numpy as np

Now let us construct a couple of one dimensional arrays for which the maximum elements are to be determined.

ar1 = np.array([[1.2, 3.4, 6.7, 8.9]], dtype = int)
ar2 = np.array([[2.1, 4.3, 5.7, 6.9]], dtype = int)

It could be noticed above that the data type being considered is int so one can very well expect the output to be stripped off the decimal numbers. Now it is time to use the maximum( ) function!

np.maximum(ar1, ar2, dtype = int)
Calculating The Maximum Of One Dimensional Arrays
Calculating the Maximum of One Dimensional Arrays

Using maximum( ) on N-Dimensional Arrays

Now that we have dealt with finding the maximum amongst the elements of one-dimensional arrays, let us in this section extend our quest by using arrays of multiple dimensions such as those given below to return their maximum elements using the maximum( ) function.

ar1 = np.array([[1.2, 3.4, 6.7, 8.9],
                [9.8, 7.6, 5.4, 3.2]], dtype = float)
ar2 = np.array([[2.1, 4.3, 5.7, 6.9],
                [9.7, 8.6, 4.5, 1.2]], dtype = float)
np.maximum(ar1, ar2, dtype = float)
Calculating The Maximum Of N Dimensional Arrays
Calculating the Maximum of N-Dimensional Arrays

The trained eyes could be observant of the similarity with the output array of the previous section that both of them are of the same dimensions as their input arrays. This gives us a clue on how to construct an output array if at all, we would like to store the result elsewhere.


Using where in maximum( ) function

We now get to the great part of using the maximum( ) function in which one can also selectively find the maximum of the given array elements confined to only a particular position within the array by exercising the where option as demonstrated below.

ar1 = np.array([[1.2, 3.4, 6.7, 8.9],
                [9.8, 7.6, 5.4, 3.2]], dtype = float)
ar2 = np.array([[2.1, 4.3, 5.7, 6.9],
                [9.7, 8.6, 4.5, 1.2]], dtype = float)
np.maximum(ar1, ar2, where = [True, True, False, True])

The above code asks the maximum( ) function to only return the maximum values of the input arrays compared in all the positions except for the third where it runs the contrarian execution.

Results Using The Where Option 1
Results Using the Where Option

Conclusion:

Now that we have reached the end of this article, hope it has elaborated on how to find the maximum of array elements using the maximum( ) function of the numpy library. Here’s another article that explains how to find the minimum of array elements using numpy in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, hasta luego!