Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23214

Real Python: NumPy's max() and maximum(): Find Extreme Values in Arrays

$
0
0

NumPy’s max() function efficiently finds maximum values within an array, making it a key tool for data analysis in Python. This tutorial guides you through using max() and maximum(), handling missing values, and explores advanced features like broadcasting for comparing arrays of different shapes.

By the end of this tutorial, you’ll understand that:

  • NumPy’s max() function finds the maximum value within a single array, working with both one-dimensional and multi-dimensional arrays.
  • Conversely, np.maximum() compares two arrays element-wise to find the maximum values.
  • np.amax() and max() are equivalent in NumPy.
  • You can use np.nanmax() to find the maximum value in an array while ignoring nan values, preventing them from affecting the result.

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:

Python
>>> 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]])
Copied!

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:

Python
>>> 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
Copied!

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().

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 ]


Viewing all articles
Browse latest Browse all 23214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>