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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-86387{{"`NumPy Amax Function`"}} python/booleans -.-> lab-86387{{"`NumPy Amax Function`"}} python/lists -.-> lab-86387{{"`NumPy Amax Function`"}} python/tuples -.-> lab-86387{{"`NumPy Amax Function`"}} python/importing_modules -.-> lab-86387{{"`NumPy Amax Function`"}} python/numerical_computing -.-> lab-86387{{"`NumPy Amax Function`"}} python/build_in_functions -.-> lab-86387{{"`NumPy Amax Function`"}} numpy/multi_array -.-> lab-86387{{"`NumPy Amax Function`"}} numpy/data_array -.-> lab-86387{{"`NumPy Amax Function`"}} numpy/bool_idx -.-> lab-86387{{"`NumPy Amax Function`"}} numpy/fancy_idx -.-> lab-86387{{"`NumPy Amax Function`"}} end

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