NumPy Amax Function

PythonPythonBeginner
Practice Now

Introduction

NumPy is a powerful library for the Python programming language that is used to perform mathematical operations, especially on arrays. NumPy provides many built-in functions, one of which is the amax() function. In this lab, we will discuss the amax() function with examples to help you understand its syntax, parameters, and usage.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import the necessary libraries

To use the amax() function, we need to import the NumPy library. In Python, we can use the import keyword to import libraries.

import numpy as np

Create an input array

We can create a NumPy array using the array() method. In this step, we will create a simple array to use for our examples.

a = np.array([[4, 5, 2], [3, 7, 1], [8, 6, 9]])

Find the maximum element of the array

The amax() function is used to find the maximum value of an array. Here is an example that shows how to use the amax() function to find the maximum value of an array:

max_value = np.amax(a)
print("The maximum value of an array: ", max_value)

Output:

The maximum value of an array:  9

Find the maximum element along an axis

The amax() function can also be used to find the maximum element along a particular axis of an array. In this example, we will use the axis parameter to determine the maximum element of a row and a column.

## Find the maximum element along a row
max_row = np.amax(a, axis=1)
print("Maximum elements along a row:\n", max_row)

## Find the maximum element along a column
max_column = np.amax(a, axis=0)
print("Maximum elements along a column:\n", max_column)

Output:

Maximum elements along a row:
 [5 7 9]

Maximum elements along a column:
 [8 7 9]

Find the maximum element with "where" parameter

The where parameter is used to find the maximum value in the specified indices. Here is an example that shows how to use the where parameter with the amax() function to find the maximum value in the specified indices:

b = np.array([[4, 5, 2], [3, 7, 1], [8, 6, 9]])
max_value = np.amax(b, where=[False, True, True])
print("The maximum value of selected indices: ", max_value)

Output:

The maximum value of selected indices:  9

Summary

In this lab, we covered the amax() function of the NumPy library. We discussed the syntax of this function with its parameters and returned values. We also provided examples of using the amax() function to find the maximum element of an array, find the maximum element along an axis, and find the maximum element with the where parameter. NumPy's amax() function is a useful tool for finding the maximum element of an array and for statistical analysis of data.

Other Python Tutorials you may like