# np.amax: What is Numpy amax() Function in Python

The numpy library provides a function to get the maximum value from the Numpy array in Python. That numpy function is amax() used to get the maximum value from a ndarray. Let’s see this in detail.

## np.amax

The np.amax() function returns the maximum of an array or maximum along the axis (if mentioned). The np.amax() function takes four arguments: arr, axis, out, and keepdims, and returns the maximum value of an array.

For a single-dimensional array, we can easily find the largest element, but for the multidimensional array, we can also find the largest element of each row and column.

### Syntax

`numpy.amax(arr, axis=None, out=None, keepdims=<no value>, initial=<no value>)`

### Parameters

The amax() function takes up to four arguments: the following.

1. arr -> This is the array from which we can find the max value.
2. axis -> This indicates the axis we want to find the largest element. Otherwise, it will consider the array as flattened. In this case, if we provide axis=0, it returns an array containing the largest element for each column. If axis=1, it returns an array containing the largest element from each row.
3. out -> This is an optional field. This indicates an alternative output array in which the result is placed.
4. keepdims -> This is an optional field. If this is set to True, then the reduced axis is left in the result as dimensions with size one. With this option, the result will broadcast correctly against an input array. If the default value is passed, then the keepdims will not be passed through to all methods of sub-classes of ndarray. However, any non-default value will be. Any exceptions will be raised if the sub-classes sum method does not implement keepdims.

### Return Value

The numpy.amax() function returns the maximum value of an array of two types.

1. Scaler -> If the axis is mentioned, None.
2. Array -> Of dimension arr.ndim-1 if axis is mentioned.

### Programming Example

#### Finding the maximum value of a 1D array

```# Importing numpy
import numpy as np

# We will create an 1D array
arr = np.array([40, 24, 14, 63, 121, 4, 64])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Now we will print the max value of this array
print("Maximum value of the given array is: ", np.amax(arr))```

#### Output

```The array is:  [ 40  24  14  63 121   4  64]
Shape of the array is :  (7,)
Maximum value of the given array is:  121
```

#### Explanation

In this program, we first declared an array, with some of the users giving random numbers. Then we have printed the shape (size) of the array. Then we have called amax() to get the maximum element from the array. We can see that the maximum element of this array is 121, and the output is also 121.

#### Finding the maximum value of Multidimensional Array in Python

```#Importing numpy
import numpy as np

#We will create a 2D array
#Of shape 4x3
arr = np.array([(14, 29, 34), (41, 55, 46), (1, 38, 29), (5, 57, 52)])
#Printing the array
print("The array is: ")
print(arr)
print("Shape of the array is: ", np.shape(arr))

#Now we will find the maximum value for some cases

#Maximum value of the whole array
print("Maximum value of the whole array is: ", np.amax(arr))

#Maximum value of each row
a = np.amax(arr, axis=1)
print("Maximum value of each row of the array is: ", a)

#Maximum value of each column
b = np.amax(arr, axis=0)
print("Maximum value of each column of the array is: ", b)
```

#### Output

```Output:
[[14 29 34]
[41 55 46]
[ 1 38 29]
[ 5 57 52]]
Shape of the array is:  (4, 3)
Maximum value of the whole array is:  57
Maximum value of each row of the array is:  [34 55 38 57]
Maximum value of each column of the array is:  [41 57 52]```

#### Explanation

In this program, we have first declared a matrix of size 4×3; you can see the shape of the matrix also, which is (4,3). Then we have called amax() to get the output of different cases.

In the first case, we have just passed the arr to amax() function, which returns the maximum value in the whole array because an array is flattened if we do not mention the axis.

In the second case, we have passed arr and axis=1, which returns an array of size 4, containing all the maximum elements from each row.

In the third case, we have passed arr and axis=0, which returns an array of size 3, containing all the maximum elements from each column.

## Numpy max Vs. amax Vs. maximum

Numpy has three different methods, which seem like they can be used for the same things – except that numpy.maximum function can only be used element-wise, while numpy.max() and numpy.amax() functions can be used on particular axes or all elements. What is the difference between all of them?

The np.max function is just an alias for np.amax(). The numpy.max() function only works on a single input array and finds the value of the maximum items in that entire array (returning a scalar). Alternatively, it takes the axis argument and will find the maximum value along an axis of the input array (returning a new array).

See the following code.

```import numpy as np

arr = np.array([[0, 11, 21],
[19, 20, 1]])

print(np.max(arr))```

#### Output

```python3 app.py
21```

The output shows that the max() value returns the maximum value from the whole array. If we specify the particular axis, it will return the maximum value according to the axis. So let’s add another parameter, axis = 1.

```# app.py

import numpy as np

arr = np.array([[0, 11, 21],
[19, 20, 1]])

print(np.max(arr, axis=1))
```

#### Output

```python3 app.py
[21 20]```

The default behavior of np.maximum is to take two arrays and compute their element-wise maximum. Here, the ‘compatible’ means that one array can be broadcast to the other.

But np.maximum is also the universal function, which means that it has other features and methods which come in useful when working with multidimensional arrays.

For example, you can compute the cumulative maximum over the array.

```# app.py

import numpy as np

arr = np.array([21, 0, 31, -41, -21, 18, 19])

print(np.maximum.accumulate(arr))
```

#### Output

```python3 app.py
[21 21 31 31 31 31 31]```

This is not possible with the np.max function. You can make np.maximum imitate np.max to a certain extent when using np.maximum.reduce function.

That’s it for this tutorial.