- numpy.maximum(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘maximum’>#
-
Element-wise maximum of array elements.
Compare two arrays and returns a new array containing the element-wise
maxima. If one of the elements being compared is a NaN, then that
element is returned. If both elements are NaNs then the first is
returned. The latter distinction is important for complex NaNs, which
are defined as at least one of the real or imaginary parts being a NaN.
The net effect is that NaNs are propagated.- Parameters:
-
- x1, x2array_like
-
The arrays holding the elements to be compared.
Ifx1.shape != x2.shape
, they must be broadcastable to a common
shape (which becomes the shape of the output). - outndarray, None, or tuple of ndarray and None, optional
-
A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or None,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs. - wherearray_like, optional
-
This condition is broadcast over the input. At locations where the
condition is True, the out array will be set to the ufunc result.
Elsewhere, the out array will retain its original value.
Note that if an uninitialized out array is created via the default
out=None
, locations within it where the condition is False will
remain uninitialized. - **kwargs
-
For other keyword-only arguments, see the
ufunc docs.
- Returns:
-
- yndarray or scalar
-
The maximum of x1 and x2, element-wise.
This is a scalar if both x1 and x2 are scalars.
See also
minimum
-
Element-wise minimum of two arrays, propagates NaNs.
fmax
-
Element-wise maximum of two arrays, ignores NaNs.
amax
-
The maximum value of an array along a given axis, propagates NaNs.
nanmax
-
The maximum value of an array along a given axis, ignores NaNs.
fmin
,amin
,nanmin
Notes
The maximum is equivalent to
np.where(x1 >= x2, x1, x2)
when
neither x1 nor x2 are nans, but it is faster and does proper
broadcasting.Examples
>>> np.maximum([2, 3, 4], [1, 5, 2]) array([2, 5, 4])
>>> np.maximum(np.eye(2), [0.5, 2]) # broadcasting array([[ 1. , 2. ], [ 0.5, 2. ]])
>>> np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) array([nan, nan, nan]) >>> np.maximum(np.Inf, 1) inf
An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can’t create a multidimensional array. And the data type must be the same.
To overcome these problems we use a third-party module called NumPy. Using NumPy we can create multidimensional arrays, and we also can use different data types.
Note: NumPy doesn’t come with python by default. So, we have to install it using pip. To install the module run the given command in the terminal.
pip install numpy
Now let’s create an array using NumPy. For doing this we need to import the module. Here we’re importing the module.
import numpy
Using the above command you can import the module.
Example 1: Now try to create a single-dimensional array.
arr = numpy.array([1, 2, 3, 4, 5])
Here, we create a single-dimensional NumPy array of integers. Now try to find the maximum element. To do this we have to use numpy.max(“array name”) function.
Syntax:
numpy.max(arr)
For finding the minimum element use numpy.min(“array name”) function.
Syntax:
numpy.min(arr)
Code:
Python3
import
numpy
arr
=
numpy.array([
1
,
5
,
4
,
8
,
3
,
7
])
max_element
=
numpy.
max
(arr)
min_element
=
numpy.
min
(arr)
print
(
'maximum element in the array is: '
,
max_element)
print
(
'minimum element in the array is: '
,
min_element)
Output:
maximum element in the array is: 8 minimum element in the array is: 1
Note: You must use numeric numbers(int or float), you can’t use string.
Example 2: Now, let’s create a two-dimensional NumPy array.
arr = numpy.array([11, 5, 7], [4, 5, 16], [7, 81, 16]]
Now using the numpy.max() and numpy.min() functions we can find the maximum and minimum element.
Here, we get the maximum and minimum value from the whole array.
Code:
Python3
import
numpy
arr
=
numpy.array([[
11
,
2
,
3
],
[
4
,
5
,
16
],
[
7
,
81
,
22
]])
max_element
=
numpy.
max
(arr)
min_element
=
numpy.
min
(arr)
print
(
'maximum element in the array is:'
,
max_element)
print
(
'minimum element in the array is:'
,
min_element)
Output:
maximum element in the array is: 81 minimum element in the array is: 2
Example 3: Now, if we want to find the maximum or minimum from the rows or the columns then we have to add 0 or 1. See how it works:
maximum_element = numpy.max(arr, 0) maximum_element = numpy.max(arr, 1)
If we use 0 it will give us a list containing the maximum or minimum values from each column. Here we will get a list like [11 81 22] which have all the maximum numbers each column.
If we use 1 instead of 0, will get a list like [11 16 81], which contain the maximum number from each row.
Code:
Python3
import
numpy
arr
=
numpy.array([[
11
,
2
,
3
],
[
4
,
5
,
16
],
[
7
,
81
,
22
]])
max_element_column
=
numpy.
max
(arr,
0
)
max_element_row
=
numpy.
max
(arr,
1
)
min_element_column
=
numpy.amin(arr,
0
)
min_element_row
=
numpy.amin(arr,
1
)
print
(
'maximum elements in the columns of the array is:'
,
max_element_column)
print
(
'maximum elements in the rows of the array is:'
,
max_element_row)
print
(
'minimum elements in the columns of the array is:'
,
min_element_column)
print
(
'minimum elements in the rows of the array is:'
,
min_element_row)
Output:
maximum elements in the columns of the array is: [11 81 22] maximum elements in the rows of the array is: [11 16 81] minimum elements in the columns of the array is: [4 2 3] minimum elements in the rows of the array is: [2 4 7]
Example 4: If we have two same shaped NumPy arrays, we can find the maximum or minimum elements. For this step, we have to numpy.maximum(array1, array2) function. It will return a list containing maximum values from each column.
Code:
Python3
import
numpy
a
=
numpy.array([
1
,
4
,
6
,
8
,
9
])
b
=
numpy.array([
5
,
7
,
3
,
9
,
22
])
print
(numpy.maximum(a, b))
Output:
[ 5 7 6 9 22]
Last Updated :
13 Aug, 2021
Like Article
Save Article
The NumPy library supports expressive, efficient numerical programming in Python. Finding extreme values is a very common requirement in data analysis. The NumPy max()
and maximum()
functions are two examples of how NumPy lets you combine the coding comfort offered by Python with the runtime efficiency you’d expect from C.
In this tutorial, you’ll learn how to:
- Use the NumPy
max()
function - Use the NumPy
maximum()
function and understand why it’s different frommax()
- Solve practical problems with these functions
- Handle missing values in your data
- Apply the same concepts to finding minimum values
This tutorial includes a very short introduction to NumPy, so even if you’ve never used NumPy before, you should be able to jump right in.
With the background provided here, you’ll be ready to continue exploring the wealth of functionality to be found in the NumPy library.
NumPy: Numerical Python
NumPy is short for Numerical Python. It’s an open source Python library that enables a wide range of applications in the fields of science, statistics, and data analytics through its support of fast, parallelized computations on multidimensional arrays of numbers. Many of the most popular numerical packages use NumPy as their base library.
Introducing NumPy
The NumPy library is built around a class named np.ndarray
and a set of methods and functions that leverage Python syntax for defining and manipulating arrays of any shape or size.
NumPy’s core code for array manipulation is written in C. You can use functions and methods directly on an ndarray
as NumPy’s C-based code efficiently loops over all the array elements in the background. NumPy’s high-level syntax means that you can simply and elegantly express complex programs and execute them at high speeds.
You can use a regular Python list
to represent an array. However, NumPy arrays are far more efficient than lists, and they’re supported by a huge library of methods and functions. These include mathematical and logical operations, sorting, Fourier transforms, linear algebra, array reshaping, and much more.
Today, NumPy is in widespread use in fields as diverse as astronomy, quantum computing, bioinformatics, and all kinds of engineering.
NumPy is used under the hood as the numerical engine for many other libraries, such as pandas and SciPy. It also integrates easily with visualization libraries like Matplotlib and seaborn.
NumPy is easy to install with your package manager, for example pip
or conda
. For detailed instructions plus a more extensive introduction to NumPy and its capabilities, take a look at NumPy Tutorial: Your First Steps Into Data Science in Python or the NumPy Absolute Beginner’s Guide.
In this tutorial, you’ll learn how to take your very first steps in using NumPy. You’ll then explore NumPy’s max()
and maximum()
commands.
Creating and Using NumPy Arrays
You’ll start your investigation with a quick overview of NumPy arrays, the flexible data structure that gives NumPy its versatility and power.
The fundamental building block for any NumPy program is the ndarray
. An ndarray
is a Python object wrapping an array of numbers. It may, in principle, have any number of dimensions of any size. You can declare an array in several ways. The most straightforward method starts from a regular Python list or tuple:
>>>
>>> import numpy as np
>>> A = np.array([3, 7, 2, 4, 5])
>>> A
array([3, 7, 2, 4, 5])
>>> B = np.array(((1, 4), (1, 5), (9, 2)))
>>> B
array([[1, 4],
[1, 5],
[9, 2]])
You’ve imported numpy
under the alias np
. This is a standard, widespread convention, so you’ll see it in most tutorials and programs.
In this example, A
is a one-dimensional array of numbers, while B
is two-dimensional.
Notice that the np.array()
factory function expects a Python list or tuple as its first parameter, so the list or tuple must therefore be wrapped in its own set of brackets or parentheses, respectively. Just throwing in an unwrapped bunch of numbers won’t work:
>>>
>>> np.array(3, 7, 2, 4, 5)
Traceback (most recent call last):
...
TypeError: array() takes from 1 to 2 positional arguments but 5 were given
With this syntax, the interpreter sees five separate positional arguments, so it’s confused.
In your constructor for array B
, the nested tuple argument needs an extra pair of parentheses to identify it, in its entirety, as the first parameter of np.array()
.
Addressing the array elements is straightforward. NumPy’s indices start at zero, like all Python sequences. By convention, a two-dimensional array is displayed so that the first index refers to the row, and the second index refers to the column. So A[0]
is the first element of the one-dimensional array A
, and B[2, 1]
is the second element in the third row of the two-dimensional array B
:
>>>
>>> A[0] # First element of A
3
>>> A[4] # Fifth and last element of A
5
>>> A[-1] # Last element of A, same as above
5
>>> A[5] # This won't work because A doesn't have a sixth element
Traceback (most recent call last):
...
IndexError: index 5 is out of bounds for axis 0 with size 5
>>> B[2, 1] # Second element in third row of B
2
So far, it seems that you’ve simply done a little extra typing to create arrays that look very similar to Python lists. But looks can be deceptive! Each ndarray
object has approximately a hundred built-in properties and methods, and you can pass it to hundreds more functions in the NumPy library.
Almost anything that you can imagine doing to an array can be achieved in a few lines of code. In this tutorial, you’ll only be using a few functions, but you can explore the full power of arrays in the NumPy API documentation.
Creating Arrays in Other Ways
You’ve already created some NumPy arrays from Python sequences. But arrays can be created in many other ways. One of the simplest is np.arange()
, which behaves rather like a souped-up version of Python’s built-in range()
function:
>>>
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(2, 3, 0.1)
array([ 2., 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
In the first example above, you only specified the upper limit of 10
. NumPy follows the standard Python convention for ranges and returns an ndarray
containing the integers 0
to 9
. The second example specifies a starting value of 2
, an upper limit of 3
, and an increment of 0.1
. Unlike Python’s standard range()
function, np.arange()
can handle non-integer increments, and it automatically generates an array with np.float
elements in this case.
NumPy’s arrays may also be read from disk, synthesized from data returned by APIs, or constructed from buffers or other arrays.
NumPy arrays can contain various types of integers, floating-point numbers, and complex numbers, but all the elements in an array must be of the same type.
You’ll start by using built-in ndarray
properties to understand the arrays A
and B
:
>>>
>>> A.size
5
>>> A.shape
(5,)
>>> B.size
6
>>> B.shape
(3, 2)
The .size
attribute counts the elements in the array, and the .shape
attribute contains an ordered tuple of dimensions, which NumPy calls axes. A
is a one-dimensional array with one row containing five elements. Because A
has only one axis, A.shape
returns a one-element tuple.
By convention, in a two-dimensional matrix, axis 0
corresponds to the rows, and axis 1
corresponds to the columns, so the output of B.shape
tells you that B
has three rows and two columns.
Python strings and lists have a very handy feature known as slicing, which allows you to select sections of a string or list by specifying indices or ranges of indices. This idea generalizes very naturally to NumPy arrays.
For example, you can extract just the parts you need from B
, without affecting the original array:
>>>
>>> B[2, 0]
9
>>> B[1, :]
array([1, 5])
In the first example above, you picked out the single element in row 2
and column 0
using B[2, 0]
. The second example uses a slice to pick out a sub-array. Here, the index 1
in B[1, :]
selects row 1
of B
. The :
in the second index position selects all the elements in that row. As a result, the expression B[1, :]
returns an array with one row and two columns, containing all the elements from row 1
of B
.
If you need to work with matrices having three or more dimensions, then NumPy has you covered. The syntax is flexible enough to cover any case. In this tutorial, though, you’ll only deal with one- and two-dimensional arrays.
If you have any questions as you play with NumPy, the official NumPy docs are thorough and well-written. You’ll find them indispensable if you do serious development using NumPy.
NumPy’s max()
: The Maximum Element in an Array
In this section, you’ll become familiar with np.max()
, a versatile tool for finding maximum values in various circumstances.
np.max()
is the tool that you need for finding the maximum value or values in a single array. Ready to give it a go?
Using max()
To illustrate the max()
function, you’re going to create an array named n_scores
containing the test scores obtained by the students in Professor Newton’s linear algebra class.
Each row represents one student, and each column contains the scores on a particular test. So column 0
contains all the student scores for the first test, column 1
contains the scores for the second test, and so on. Here’s the n_scores
array:
>>>
>>> import numpy as np
>>> n_scores = np.array([
... [63, 72, 75, 51, 83],
... [44, 53, 57, 56, 48],
... [71, 77, 82, 91, 76],
... [67, 56, 82, 33, 74],
... [64, 76, 72, 63, 76],
... [47, 56, 49, 53, 42],
... [91, 93, 90, 88, 96],
... [61, 56, 77, 74, 74],
... ])
You can copy and paste this code into your Python console if you want to follow along. To simplify the formatting before copying, click >>>
at the top right of the code block. You can do the same with any of the Python code in the examples.
Once you’ve done that, the n_scores
array is in memory. You can ask the interpreter for some of its attributes:
>>>
>>> n_scores.size
40
>>> n_scores.shape
(8, 5)
The .shape
and .size
attributes, as above, confirm that you have 8
rows representing students and 5
columns representing tests, for a total of 40
test scores.
Suppose now that you want to find the top score achieved by any student on any test. For Professor Newton’s little linear algebra class, you could find the top score fairly quickly just by examining the data. But there’s a quicker method that’ll show its worth when you’re dealing with much larger datasets, containing perhaps thousands of rows and columns.
Try using the array’s .max()
method:
The .max()
method has scanned the whole array and returned the largest element. Using this method is exactly equivalent to calling np.max(n_scores)
.
But perhaps you want some more detailed information. What was the top score for each test?
Here you can use the axis
parameter:
>>>
>>> n_scores.max(axis=0)
array([91, 93, 90, 91, 96])
The new parameter axis=0
tells NumPy to find the largest value out of all the rows. Since n_scores
has five columns, NumPy does this for each column independently. This produces five numbers, each of which is the maximum value in that column.
The axis
parameter uses the standard convention for indexing dimensions. So axis=0
refers to the rows of an array, and axis=1
refers to the columns.
The top score for each student is just as easy to find:
>>>
>>> n_scores.max(axis=1)
array([83, 57, 91, 82, 76, 56, 96, 77])
This time, NumPy has returned an array with eight elements, one per student. The n_scores
array contains one row per student. The parameter axis=1
told NumPy to find the maximum value for each student, across the columns. Therefore, each element of the output contains the highest score attained by the corresponding student.
Perhaps you want the top scores per student, but you’ve decided to exclude the first and last tests. Slicing does the trick:
>>>
>>> filtered_scores = n_scores[:, 1:-1]
>>> filtered_scores.shape
(8, 3)
>>> filtered_scores
array([72, 75, 51],
[53, 57, 56],
[77, 82, 91],
[56, 82, 33],
[76, 72, 63],
[56, 49, 53],
[93, 90, 88],
[56, 77, 74]])
>>> filtered_scores.max(axis=1)
array([75, 57, 91, 82, 76, 56, 93, 77])
You can understand the slice notation n_scores[:, 1:-1]
as follows. The first index range, represented by the lone :
, selects all the rows in the slice.
The second index range after the comma, 1:-1
, tells NumPy to take the columns, starting at column 1
and ending 1
column before the last. The result of the slice is stored in a new array named filtered_scores
.
With a bit of practice, you’ll learn to do array slicing on the fly, so you won’t need to create the intermediate array filtered_scores
explicitly:
>>>
>>> n_scores[:, 1:-1].max(axis=1)
array([75, 57, 91, 82, 76, 56, 93, 77])
Here you’ve performed the slice and the method call in a single line, but the result is the same. NumPy returns the per-student set of maximum n_scores
for the restricted set of tests.
Handling Missing Values in np.max()
So now you know how to find maximum values in any completely filled array. But what happens when a few array values are missing? This is pretty common with real-world data.
To illustrate, you’ll create a small array containing a week’s worth of daily temperature readings, in Celsius, from a digital thermometer, starting on Monday:
>>>
>>> temperatures_week_1 = np.array([7.1, 7.7, 8.1, 8.0, 9.2, np.nan, 8.4])
>>> temperatures_week_1.size
7
It seems the thermometer had a malfunction on Saturday, and the corresponding temperature value is missing, a situation indicated by the np.nan
value. This is the special value Not a Number, which is commonly used to mark missing values in real-world data applications.
So far, so good. But a problem arises if you innocently try to apply .max()
to this array:
>>>
>>> temperatures_week_1.max()
nan
Since np.nan
reports a missing value, NumPy’s default behavior is to flag this by reporting that the maximum, too, is unknown. For some applications, this makes perfect sense. But for your application, perhaps you’d find it more useful to ignore the Saturday problem and get a maximum value from the remaining, valid readings. NumPy has provided the np.nanmax()
function to take care of such situations:
>>>
>>> np.nanmax(temperatures_week_1)
9.2
This function ignores any nan
values and returns the largest numerical value, as expected.
Notice that np.nanmax()
is a function in the NumPy library, not a method of the ndarray
object.
You’ve now seen the most common examples of NumPy’s maximum-finding capabilities for single arrays. But there are a few more NumPy functions related to maximum values that are worth knowing about.
For example, instead the maximum values in an array, you might want the indices of the maximum values. Let’s say you want to use your n_scores
array to identify the student who did best on each test. The .argmax()
method is your friend here:
>>>
>>> n_scores.argmax(axis=0)
array([6, 6, 6, 2, 6])
It appears that student 6
obtained the top score on every test but one. Student 2
did best on the fourth test.
You’ll recall that you can also apply np.max()
as a function of the NumPy package, rather than as a method of a NumPy array. In this case, the array must be supplied as the first argument of the function.
For historical reasons, the package-level function np.max()
has an alias, np.amax()
, which is identical in every respect apart from the name:
>>>
>>> n_scores.max(axis=1)
array([83, 57, 91, 82, 76, 56, 96, 77])
>>> np.max(n_scores, axis=1)
array([83, 57, 91, 82, 76, 56, 96, 77])
>>> np.amax(n_scores, axis=1)
array([83, 57, 91, 82, 76, 56, 96, 77])
In the code above, you’ve called .max()
as a method of the n_scores
object, and as a stand-alone library function with n_scores
as its first parameter. You’ve also called the alias np.amax()
in the same way. All three calls produce exactly the same results.
Now you’ve seen how to use np.max()
, np.amax()
, or .max()
to find maximum values for an array along various axes. You’ve also used np.nanmax()
to find the maximum values while ignoring nan
values, as well as np.argmax()
or .argmax()
to find the indices of the maximum values.
You won’t be surprised to learn that NumPy has an equivalent set of minimum functions: np.min()
, np.amin()
, .min()
, np.nanmin()
, np.argmin()
, and .argmin()
. You won’t deal with those here, but they behave exactly like their maximum cousins.
NumPy’s maximum()
: Maximum Elements Across Arrays
Another common task in data science involves comparing two similar arrays. NumPy’s maximum()
function is the tool of choice for finding maximum values across arrays. Since maximum()
always involves two input arrays, there’s no corresponding method. The np.maximum()
function expects the input arrays as its first two parameters.
Using np.maximum()
Continuing with the previous example involving class scores, suppose that Professor Newton’s colleague—and archrival—Professor Leibniz is also running a linear algebra class with eight students. Construct a new array with the values for Leibniz’s class:
>>>
>>> l_scores = np.array([
... [87, 73, 71, 59, 67],
... [60, 53, 82, 80, 58],
... [92, 85, 60, 79, 77],
... [67, 79, 71, 69, 87],
... [86, 91, 92, 73, 61],
... [70, 66, 60, 79, 57],
... [83, 51, 64, 63, 58],
... [89, 51, 72, 56, 49],
... ])
>>> l_scores.shape
(8, 5)
The new array, l_scores
, has the same shape as n_scores
.
You’d like to compare the two classes, student by student and test by test, to find the higher score in each case.
NumPy has a function, np.maximum()
, specifically designed for comparing two arrays in an element-by-element manner.
Check it out in action:
>>>
>>> np.maximum(n_scores, l_scores)
array([[87, 73, 75, 59, 83],
[60, 53, 82, 80, 58],
[92, 85, 82, 91, 77],
[67, 79, 82, 69, 87],
[86, 91, 92, 73, 76],
[70, 66, 60, 79, 57],
[91, 93, 90, 88, 96],
[89, 56, 77, 74, 74]])
If you visually check the arrays n_scores
and l_scores
, then you’ll see that np.maximum()
has indeed picked out the higher of the two scores for each [row, column] pair of indices.
What if you only want to compare the best test results in each class? You can combine np.max()
and np.maximum()
to get that effect:
>>>
>>> best_n = n_scores.max(axis=0)
>>> best_n
array([91, 93, 90, 91, 96])
>>> best_l = l_scores.max(axis=0)
>>> best_l
array([92, 91, 92, 80, 87])
>>> np.maximum(best_n, best_l)
array([92, 93, 92, 91, 96])
As before, each call to .max()
returns an array of maximum scores for all the students in the relevant class, one element for each test. But this time, you’re feeding those returned arrays into the maximum()
function, which compares the two arrays and returns the higher score for each test across the arrays.
You can combine those operations into one by dispensing with the intermediate arrays, best_n
and best_l
:
>>>
>>> np.maximum(n_scores.max(axis=0), l_scores.max(axis=0))
array([91, 93, 90, 91, 96])
This gives the same result as before, but with less typing. You can choose whichever method you prefer.
Handling Missing Values in np.maximum()
Remember the temperatures_week_1
array from an earlier example? If you use a second week’s temperature records with the maximum()
function, you may spot a familiar problem.
First, you’ll create a new array to hold the new temperatures:
>>>
>>> temperatures_week_2 = np.array(
... [7.3, 7.9, np.nan, 8.1, np.nan, np.nan, 10.2]
... )
There are missing values in the temperatures_week_2
data, too. Now see what happens if you apply the np.maximum
function to these two temperature arrays:
>>>
>>> np.maximum(temperatures_week_1, temperatures_week_2)
array([ 7.3, 7.9, nan, 8.1, nan, nan, 10.2])
All the nan
values in both arrays have popped up as missing values in the output. There’s a good reason for NumPy’s approach to propagating nan
. Often it’s important for the integrity of your results that you keep track of the missing values, rather than brushing them under the rug. But here, you just want to get the best view of the weekly maximum values. The solution, in this case, is another NumPy package function, np.fmax()
:
>>>
>>> np.fmax(temperatures_week_1, temperatures_week_2)
array([ 7.3, 7.9, 8.1, 8.1, 9.2, nan, 10.2])
Now, two of the missing values have simply been ignored, and the remaining floating-point value at that index has been taken as the maximum. But the Saturday temperature can’t be fixed in that way, because both source values are missing. Since there’s no reasonable value to insert here, np.fmax()
just leaves it as a nan
.
Just as np.max()
and np.nanmax()
have the parallel minimum functions np.min()
and np.nanmin()
, so too do np.maximum()
and np.fmax()
have corresponding functions, np.minimum()
and np.fmin()
, that mirror their functionality for minimum values.
Advanced Usage
You’ve now seen examples of all the basic use cases for NumPy’s max()
and maximum()
, plus a few related functions.
Now you’ll investigate some of the more obscure optional parameters to these functions and find out when they can be useful.
Reusing Memory
When you call a function in Python, a value or object is returned. You can use that result immediately by printing it or writing it to disk, or by feeding it directly into another function as an input parameter. You can also save it to a new variable for future reference.
If you call the function in the Python REPL but don’t use it in one of those ways, then the REPL prints out the return value on the console so that you’re aware that something has been returned. All of this is standard Python stuff, and not specific to NumPy.
NumPy’s array functions are designed to handle huge inputs, and they often produce huge outputs. If you call such a function many hundreds or thousands of times, then you’ll be allocating very large amounts of memory. This can slow your program down and, in an extreme case, might even cause a memory or stack overflow.
This problem can be avoided by using the out
parameter, which is available for both np.max()
and np.maximum()
, as well as for many other NumPy functions. The idea is to pre-allocate a suitable array to hold the function result, and keep reusing that same chunk of memory in subsequent calls.
You can revisit the temperature problem to create an example of using the out
parameter with the np.max()
function. You’ll also use the dtype
parameter to control the type of the returned array:
>>>
>>> temperature_buffer = np.empty(7, dtype=np.float32)
>>> temperature_buffer.shape
(7,)
>>> np.maximum(temperatures_week_1, temperatures_week_2, out=temperature_buffer)
array([ 7.3, 7.9, nan, 8.1, nan, nan, 10.2], dtype=float32)
The initial values in temperature_buffer
don’t matter, since they’ll be overwritten. But the array’s shape is important in that it must match the output shape. The displayed result looks like the output that you received from the original np.maximum()
example. So what’s changed? The difference is that you now have the same data stored in temperature_buffer
:
>>>
>>> temperature_buffer
array([ 7.3, 7.9, nan, 8.1, nan, nan, 10.2], dtype=float32)
The np.maximum()
return value has been stored in the temperature_buffer
variable, which you previously created with the right shape to accept that return value. Since you also specified dtype=np.float32
when you declared this buffer, NumPy will do its best to convert the output data to that type.
Remember to use the buffer contents before they’re overwritten by the next call to this function.
Filtering Arrays
Another parameter that’s occasionally useful is where
. This applies a filter to the input array or arrays, so that only those values for which the where
condition is True
will be included in the comparison. The other values will be ignored, and the corresponding elements of the output array will be left unaltered. In most cases, this will leave them holding arbitrary values.
For the sake of the example, suppose you’ve decided, for whatever reason, to ignore all scores less than 60
for calculating the per-student maximum values in Professor Newton’s class. Your first attempt might go like this:
>>>
>>> n_scores
array([[63, 72, 75, 51, 83],
[44, 53, 57, 56, 48],
[71, 77, 82, 91, 76],
[67, 56, 82, 33, 74],
[64, 76, 72, 63, 76],
[47, 56, 49, 53, 42],
[91, 93, 90, 88, 96],
[61, 56, 77, 74, 74]])
>>> n_scores.max(axis=1, where=(n_scores >= 60))
ValueError: reduction operation 'maximum' does not have an identity,
so to use a where mask one has to specify 'initial'
The problem here is that NumPy doesn’t know what to do with the students in rows 1
and 5
, who didn’t achieve a single test score of 60
or better. The solution is to provide an initial
parameter:
>>>
>>> n_scores.max(axis=1, where=(n_scores >= 60), initial=60)
array([83, 60, 91, 82, 76, 60, 96, 77])
With the two new parameters, where
and initial
, n_scores.max()
considers only the elements greater than or equal to 60
. For the rows where there is no such element, it returns the initial
value of 60
instead. So the lucky students at indices 1
and 5
got their best score boosted to 60
by this operation!
The original n_scores
array is untouched.
Comparing Differently Shaped Arrays With Broadcasting
You’ve learned how to use np.maximum()
to compare arrays with identical shapes. But it turns out that this function, along with many others in the NumPy library, is much more versatile than that. NumPy has a concept called broadcasting that provides a very useful extension to the behavior of most functions involving two arrays, including np.maximum()
.
Whenever you call a NumPy function that operates on two arrays, A
and B
, it checks their .shape
properties to see if they’re compatible. If they have exactly the same .shape
, then NumPy just matches the arrays element by element, pairing up the element at A[i, j]
with the element at B[i, j]
. np.maximum()
works like this too.
Broadcasting enables NumPy to operate on two arrays with different shapes, provided there’s still a sensible way to match up pairs of elements. The simplest example of this is to broadcast a single element over an entire array. You’ll explore broadcasting by continuing the example of Professor Newton and his linear algebra class. Suppose he asks you to ensure that none of his students receives a score below 75
. Here’s how you might do it:
>>>
>>> np.maximum(n_scores, 75)
array([[75, 75, 75, 75, 83],
[75, 75, 75, 75, 75],
[75, 77, 82, 91, 76],
[75, 75, 82, 75, 75],
[75, 76, 75, 75, 76],
[75, 75, 75, 75, 75],
[91, 93, 90, 88, 96],
[75, 75, 77, 75, 75]])
You’ve applied the np.maximum()
function to two arguments: n_scores
, whose .shape
is (8, 5), and the single scalar parameter 75
. You can think of this second parameter as a 1 × 1 array that’ll be stretched inside the function to cover eight rows and five columns. The stretched array can then be compared element by element with n_scores
, and the pairwise maximum can be returned for each element of the result.
The result is the same as if you had compared n_scores
with an array of its own shape, (8, 5), but with the value 75
in each element.
This stretching is just conceptual—NumPy is smart enough to do all this without actually creating the stretched array. So you get the notational convenience of this example without compromising efficiency.
You can do much more with broadcasting. Professor Leibniz has noticed Newton’s skulduggery with his best_n_scores
array, and decides to engage in a little data manipulation of her own.
Leibniz’s plan is to artificially boost all her students’ scores to be at least equal to the average score for a particular test. This will have the effect of increasing all the below-average scores—and thus produce some quite misleading results! How can you help the professor achieve her somewhat nefarious ends?
Your first step is to use the array’s .mean()
method to create a one-dimensional array of means per test. Then you can use np.maximum()
and broadcast this array over the entire l_scores
matrix:
>>>
>>> mean_l_scores = l_scores.mean(axis=0, dtype=np.integer)
>>> mean_l_scores
array([79, 68, 71, 69, 64])
>>> np.maximum(mean_l_scores, l_scores)
array([[87, 73, 71, 69, 67],
[79, 68, 82, 80, 64],
[92, 85, 71, 79, 77],
[79, 79, 71, 69, 87],
[86, 91, 92, 73, 64],
[79, 68, 71, 79, 64],
[83, 68, 71, 69, 64],
[89, 68, 72, 69, 64]])
The broadcasting happens in the highlighted function call. The one-dimensional mean_l_scores
array has been conceptually stretched to match the two-dimensional l_scores
array. The output array has the same .shape
as the larger of the two input arrays, l_scores
.
Following Broadcasting Rules
So, what are the rules for broadcasting? A great many NumPy functions accept two array arguments. np.maximum()
is just one of these.
Arrays that can be used together in such functions are termed compatible, and their compatibility depends on the number and size of their dimensions—that is, on their .shape
.
The simplest case occurs if the two arrays, say A
and B
, have identical shapes. Each element in A
is matched, for the function’s purposes, to the element at the same index address in B
.
Broadcasting rules get more interesting when A
and B
have different shapes. The elements of compatible arrays must somehow be unambiguously paired together so that each element of the larger array can interact with an element of the smaller array. The output array will have the .shape
of the larger of the two input arrays. So compatible arrays must follow these rules:
-
If one array has fewer dimensions than the other, only the trailing dimensions are matched for compatibility. The trailing dimensions are those that are present in the
.shape
of both arrays, counting from the right. So ifA.shape
is(99, 99, 2, 3)
andB.shape
is(2, 3)
, thenA
andB
are compatible because(2, 3)
are the trailing dimensions of each. You can completely ignore the two leftmost dimensions ofA
. -
Even if the trailing dimensions aren’t equal, the arrays are still compatible if one of those dimensions is equal to
1
in either array. So ifA.shape
is(99, 99, 2, 3)
as before andB.shape
is(1, 99, 1, 3)
or(1, 3)
or(1, 2, 1)
or(1, 1)
, thenB
is still compatible withA
in each case.
You can get a feel for the broadcasting rules by playing around in the Python REPL. You’ll be creating some toy arrays to illustrate how broadcasting works and how the output array is generated:
>>>
>>> A = np.arange(24).reshape(2, 3, 4)
>>> A
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]],
[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
>>> A.shape
(2, 3, 4)
>>> B = np.array(
... [
... [[-7, 11, 10, 2], [-6, 7, -2, 14], [ 7, 4, 4, -1]],
... [[18, 5, 22, 7], [25, 8, 15, 24], [31, 15, 19, 24]],
... ]
... )
>>> B.shape
(2, 3, 4)
>>> np.maximum(A, B)
array([[[ 0, 11, 10, 3], [ 4, 7, 6, 14], [ 8, 9, 10, 11]],
[[18, 13, 22, 15], [25, 17, 18, 24], [31, 21, 22, 24]]])
There’s nothing really new to see here yet. You’ve created two arrays of identical .shape
and applied the np.maximum()
operation to them. Notice that the handy .reshape()
method lets you build arrays of any shape. You can verify that the result is the element-by-element maximum of the two inputs.
The fun starts when you experiment with comparing two arrays of different shapes. Try slicing B
to make a new array, C
:
>>>
>>> C = B[:, :1, :]
>>> C
array([[[-7, 11, 10, 2]],
[[18, 5, 22, 7]]])
>>> C.shape
(2, 1, 4)
>>> np.maximum(A, C)
array([[[ 0, 11, 10, 3], [ 4, 11, 10, 7], [ 8, 11, 10, 11]],
[[18, 13, 22, 15], [18, 17, 22, 19], [20, 21, 22, 23]]]))
The two arrays, A
and C
, are compatible because the new array’s second dimension is 1
, and the other dimensions match.
Notice that the .shape
of the result of the maximum()
operation is the same as A.shape
. That’s because C
, the smaller array, is being broadcast over A
. The result of a broadcast operation between arrays will always have the .shape
of the larger array.
Now you can try an even more radical slicing of B
:
>>>
>>> D = B[:, :1, :1]
>>> D
array([[[-7]],[[18]]])
>>> D.shape
(2, 1, 1)
>>> np.maximum(A, D)
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]],
[[18, 18, 18, 18], [18, 18, 18, 19], [20, 21, 22, 23]]])
Once again, the trailing dimensions of A
and D
are all either equal or 1
, so the arrays are compatible and the broadcast works. The result has the same .shape
as A
.
Perhaps the most extreme type of broadcasting occurs when one of the array parameters is passed as a scalar:
>>>
>>> np.maximum(A, 10)
array([[[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 11]],
[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
NumPy automatically converts the second parameter, 10
, to an array([10])
with .shape
(1,)
, determines that this converted parameter is compatible with the first, and duly broadcasts it over the entire 2 × 3 × 4 array A
.
Finally, here’s a case where broadcasting fails:
>>>
>>> E = B[:, 1:, :]
>>> E
array([[[-6, 7, -2, 14], [ 7, 4, 4, -1]],
[[25, 8, 15, 24], [31, 15, 19, 24]]])
>>> E.shape
(2, 2, 4)
>>> np.maximum(A, E)
Traceback (most recent call last):
...
ValueError: operands could not be broadcast together with shapes (2,3,4) (2,2,4)
If you refer back to the broadcasting rules above, you’ll see the problem: the second dimensions of A
and E
don’t match, and neither is equal to 1
, so the two arrays are incompatible.
You can read more about broadcasting in Look Ma, No For-Loops: Array Programming With NumPy.
There’s also a good description of the rules in the NumPy docs.
The broadcasting rules can be confusing, so it’s a good idea to play around with some toy arrays until you get a feel for how it works!
Conclusion
In this tutorial, you’ve explored the NumPy library’s max()
and maximum()
operations to find the maximum values within or across arrays.
Here’s what you’ve learned:
- Why NumPy has its own
max()
function, and how you can use it - How the
maximum()
function differs frommax()
, and when it’s needed - Which practical applications exist for each function
- How you can handle missing data so your results make sense
- How you can apply your knowledge to the complementary task of finding minimum values
Along the way, you’ve learned or refreshed your knowledge of the basics of NumPy syntax. NumPy is a hugely popular library because of its powerful support for array operations.
Now that you’ve mastered the details of NumPy’s max()
and maximum()
, you’re ready to use them in your applications, or continue learning about more of the hundreds of array functions supported by NumPy.
If you’re interested in using NumPy for data science, then you’ll also want to investigate pandas, a very popular data-science library built on top of NumPy. You can learn about it in The Pandas DataFrame: Make Working With Data Delightful. And if you want to produce compelling images from data, take a look at Python Plotting With Matplotlib (Guide).
The applications of NumPy are limitless. Wherever your NumPy adventure takes you next, go forth and matrix-multiply!
Чтобы получить максимальное значение массива Numpy в Python, вы можете использовать функцию numpy function numpy.max().
Синтаксис
Синтаксис функции max() приведен ниже.
max_value = numpy.max(arr)
Передайте массив numpy в качестве аргумента функции numpy.max(), и эта функция вернет максимальное значение.
Пример 1
В этом примере мы возьмем массив numpy со случайными числами, а затем найдем максимум массива с помощью функции numpy.max().
import numpy as np arr = np.random.randint(10, size=(4,5)) print(arr) #find maximum value max_value = np.max(arr) print('Maximum value of the array is',max_value)
Вывод:
[[3 2 2 2 2] [5 7 0 4 5] [8 1 4 8 4] [2 0 7 2 1]] Maximum value of the array is 8
Пример 2: с плавающими значениями
В следующем примере мы возьмем массив numpy со случайными значениями с плавающей запятой, а затем найдем максимум массива с помощью функции max().
import numpy as np arr = np.random.rand(6).reshape(2,3) print(arr) #find maximum value max_value = np.max(arr) print('Maximum value of the array is',max_value)
Вывод:
[[0.79794919 0.8307877 0.83587084] [0.44121629 0.69983604 0.30791677]] Maximum value of the array is 0.8358708362776799
В этом учебнике Numpy по примерам Python мы узнали, как найти максимальное значение с помощью встроенной функции max() с помощью примеров.
Максимальное значение массива вдоль оси
Вы можете найти максимальное или самое большое значение массива Numpy не только во всем массиве numpy, но также вдоль определенной оси или набора осей.
Чтобы получить максимальное значение массива Numpy вдоль оси, используйте функцию numpy.amax().
Синтаксис
Синтаксис функции приведен ниже.
max_value = numpy.amax(arr, axis)
Если вы не укажете ось, возвращается максимум массива.
Вы можете указать ось или оси для работы. Если ось является кортежем целых чисел, представляющих оси, то по этим указанным нескольким осям выбирается максимум.
Пример 1
В следующем примере мы возьмем массив numpy со случайными числами, а затем найдем максимум массива вдоль оси с помощью amax().
Найдем максимальное значение по оси = 0 и оси = 1 отдельно.
import numpy as np # 2D array => 2 axes arr = np.random.randint(10, size=(4,5)) print(arr) #find maximum value along axis=0 amax_value = np.amax(arr, axis=0) print('Maximum value of the array along axis=0 is:') print(amax_value) #find maximum value along axis=1 amax_value = np.amax(arr, axis=1) print('Maximum value of the array along axis=1 is:') print(amax_value)
Вывод:
[[4 8 8 9 4] [8 5 2 4 0] [2 3 3 8 7] [6 6 5 0 4]] Maximum value of the array along axis=0 is: [8 8 8 9 7] Maximum value of the array along axis=1 is: [9 8 8 6]
Пример 2: по нескольким осям
Как уже упоминалось, мы можем вычислить максимальное значение по нескольким осям. Предоставьте всю ось в виде кортежа, вдоль которого вы хотите найти максимальное значение.
import numpy as np # 2D array => 2 axes arr = np.random.randint(9, size=(2,2,4)) print(arr) # find maximum value along axis=0,2 amax_value = np.amax(arr, axis=(0, 2)) print('Maximum value of the array along axis=(0,2) is:') print(amax_value)
Вывод:
[[[5 6 1 2] [5 5 3 6]] [[5 2 3 3] [7 5 7 4]]] Maximum value of the array along axis=(0,2) is: [6 7]
В этом учебнике Numpy на примерах Python мы узнали, как найти максимальное значение массива numpy вдоль одной оси или нескольких осей вместе взятых.
This div height required for enabling the sticky sidebar
Beginners always face difficulty in finding the max and min Value of Numpy. In fact, the max and min are very useful in finding statistics. Therefore in this entire tutorial, you will know how to find the max and min value of Numpy and its index for both the one-dimensional and multi-dimensional array.
1. Max and Min Value for the One Dimensional (1-D) array
Let’s create a 1-D NumPy array using the numpy.array() method. The array() method accepts the list of all values you want to create NumPy array as an argument.
#1-D array
import numpy as np
array = np.array([19,5,10,1,9,17,50,19,25,50])
print(array)
Finding the Maximum Value
To find the max value you have to use the max() method. Just pass the input array as an argument inside the max() method.
max = np.max(array)
print("The maximum value in the array is :",max)
Index for the Maximum Value
To find the index for the maximum value you have to pass a specific condition as the argument inside the numpy.where() method.
#Index of the Maximum element
conditon = (array == max)
result = np.where(conditon)
print("Arrays for the max element:",result)
print("List for the maximum value indexes:",result[0])
Please note that. If there are duplicate values in the array then the output will be a list of the same maximum values.
Finding the Minimum Value
In the same way, You can find the minimum value using the numpy.min() method.
# Minimum Value
min = np.min(array)
print("The minimum value in the array is :",min)
Index of the Minimum Value
The concept to find the index of the min value is the same as finding the maximum values. You have to just change the condition inside the numpy.where() method.
# Index of the Minimum element
conditon = (array == min)
result = np.where(conditon)
print("Arrays for the min element:",result)
print("List for the min value indexes:",result[0])
2. Max and Min Value for the Two Dimensional (2-D) array
There are three ways you can find the max and min value of the NumPy array.
- Maximum and Minumum in the entire array
- Max and Min values in each column
- Maximum and Minimum values in each row.
Let’s create a two-dimensional before finding max and min values. I am randomly generating a 2-D array of size 4×3.
#2-D Array
array_2d = np.arange(12).reshape(4,3)
print(array_2d)
Finding the Max Value in the entire array
You can find the maximum value in the entire array using the same numpy.max() method just like you have used in finding the max in 1D. It will find the lowest element and gives the output.
max_2d = np.max(array_2d)
print("The maximum value for the 2D-array:",max_2d)
Maximum Value in Each Column and Row
Here you have to use an extra argument and it is axis = 0 or axis =1.
Max Value in Column
# maximum value in each column
max_in_column = np.max(array_2d,axis=0)
print(max_in_column)
Max Value in Row
# maximum value in each row
max_in_row = np.max(array_2d,axis=1)
print(max_in_row)
Here I am using the same method max() but now I am passing axis =0 to tell the interpreter to traverse along with the columns and axis =1 to traverse along the columns.
Finding the Index for the Max Value in 2D
You can easily find the index of the max value in a 1-dimensional NumPy array. But for the 2D array, you have to use Numpy module unravel_index. It will easily find the Index of the Max and Min value.
from numpy import unravel_index
result = unravel_index(np.max(array_2d),array_2d.shape)
print("Index for the Maximum Value in the 2D Array is:",result)
Here I am passing the two arguments inside the unravel_index() method one is the maximum value of the array and the shape of the array. Here In our case, the shape of the array is 4 rows and 3 columns.
Finding the Min Value in the entire array
To find the minimum value inside the array you have to use the numpy.min() method and pass the array.
#Minimum Element in the 2D- Array
min_2d = np.min(array_2d)
print("The minimum value for the 2D-array:",min_2d)
Minimum Value in Each Column and Row
Min Value in Column
# minimum value in each column
min_in_column = np.min(array_2d,axis=0)
print(min_in_column)
Min Value in Row
# minimum value in each row
min_in_row = np.min(array_2d,axis=1)
print(min_in_row)
To find the min value in each column and row you have to just change the value of the axis, axis = 0 for the column, and axis =1 for the row in the min() method.
Index for the Min Value in 2D
Just like you have learned how to find the index of max value in 2D using the unravel_index() method. Here you will also use that. You have to pass the minimum value and the shape of the array as the arguments.
index_of_min = unravel_index(np.min(array_2d),array_2d.shape) print("Index for the Minimum Value in the 2D Array is:",index_of_min)
Other Question
Question: Difference between amax() and max()
Many data science learner readers have asked us what is the difference between the two methods amax() and max(). The answer is that np.max() is an alias of the method amax(). They both are able to compute the maximum elements of any input array. You can also find the maximum value along the axis.
If you run the method then you can see both are calling the same function.
import numpy as np
print(np.max)
Output
<function numpy.core.fromnumeric.amax>
import numpy as np
print(np.amax)
Output
<function numpy.core.fromnumeric.amax>
Source:
numpy.ndarray.min
numpy.ndarray.max
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.
Something went wrong.