# NumPy amax – Maximum of an array along an axis Hello and welcome to this tutorial on Numpy amax. In this tutorial, we will be learning about the NumPy amax() method and also seeing a lot of examples regarding the same. So let us begin!

Also read: NumPy fmax(): Element-wise maximum of array elements

## What is NumPy amax?

The `amax()` method in NumPy is a function that returns the maximum of the array elements. It can be the maximum of all the array elements, the maximum of the array elements along the rows, or the maximum of the array elements along the columns.

In Python, NaN means Not a Number. If in the input data, any one element is NaN, then the maximum value will be NaN as well.

We will see the examples for each of these in the upcoming section of this tutorial.

## Syntax of NumPy amax

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

Returns:

The maximum of a. If a is a scalar, the result is also a scalar, otherwise, it is an array.

## Examples of Using Numpy Amax Function

Let’s get right into the different examples of using numpy.amax() function.

### Maximum of a 1-dimensional array

```import numpy as np

arr = [3, 6, 22, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [3, 6, 22, 10, 84]
Maximum value = 84
```

### Maximum of a 2-dimensional array

```import numpy as np

arr = [[10, 36], [4, 16]]
# using amax method to compute the maximum
ans = np.amax(arr)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [[10, 36], [4, 16]]
Maximum value = 36
```

Since this is a 2-d array, it is first flattened row-wise like this: [10, 36, 4, 16], and then the maximum is calculated.

### Maximum along axis=0

```import numpy as np

arr = [[10, 36], [4, 16]]
# using amax method to compute the maximum
ans = np.amax(arr, axis=0)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [[10, 36], [4, 16]]
Maximum value = [10 36]
```

Here, the maximum is computed along the columns as:

```ans = max(arr, arr) = max(10, 4) = 10
ans = max(arr, arr) = max(36, 16) = 36
```

### Maximum along axis=1

```import numpy as np

arr = [[10, 36], [4, 16]]
# using amax method to compute the maximum
ans = np.amax(arr, axis=1)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [[10, 36], [4, 16]]
Maximum value = [36 16]
```

In this case, the maximum is computed along the rows as follows:

```ans = max(arr, arr) = max(10, 36) = 36
ans = max(arr, arr) = max(4, 16) = 16
```

### Maximum of an array containing NaN

```import numpy as np

arr = [3, 6, np.nan, 22, np.nan, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [3, 6, nan, 22, nan, 10, 84]
Maximum value = nan
```

As stated earlier in this tutorial, if an array contains NaN, then its maximum value is also said to be NaN as can be seen in the above example.

### Maximum of an array given an initial value

```import numpy as np

arr = [3, 6, 22, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr, initial=100)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [3, 6, 22, 10, 84]
Maximum value = 100
```

Here, we have mentioned an initial value of 100. This value is compared with all the elements in the array to find the maximum.
Here, since 100 is the highest value among all the values, it is returned.

### Maximum of an array using only selected elements

To find the maximum of only some selective values in the array, we can pass the where argument to the numpy.amax() function.

```import numpy as np

arr = [3, 6, 22, 10, 84]
# using amax method to compute the maximum
ans = np.amax(arr, where=[False, False, True, True, False], initial=-1)
print("arr =", arr)
print("Maximum value =", ans)
```

Output:

```arr = [3, 6, 22, 10, 84]
Maximum value = 22
```

Here in the where list only index 2 and 3 are True, the rest are False. This implies that the amax() method has to find the maximum of only elements at index 2 and 3 in arr and ignore the remaining elements.
Hence, max(22, 10) = 10 which is returned as the answer.

## Conclusion

That’s all! In this tutorial, we learned about the Numpy amax method and practiced different types of examples using the same.