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.
Free Bonus:Click here to get access to a free NumPy Resources Guide that points you to the best tutorials, videos, and books for improving your NumPy skills.
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:
>>> importnumpyasnp>>> A=np.array([3,7,2,4,5])>>> Aarray([3, 7, 2, 4, 5])>>> B=np.array(((1,4),(1,5),(9,2)))>>> Barray([[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
:
Read the full article at https://realpython.com/numpy-max-maximum/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]