The Python numpy max() function finds the maximum element in a given array along the specified axis.

## Syntax

The syntax of this statistical Python numpy max() method is

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

Here, a is a ndarray that is mandatory, and all the other arguments are optional.

## Python numpy max Example

In this example, we declare a one-dimensional ndarray. We use this max() method to find the maximum item among them.

import numpy as np a = np.array([5, 11, 1, 99, 77, 6]) b = np.max(a) print(b)

`99`

## Two-Dimensional example

In this program, we will declare a two-dimensional square matrix to find the least value among all the ndarray rows and columns.

import numpy as np a = np.array([[40, 60], [85, 50]]) b = np.max(a) print(b)

`85`

### Python numpy max Row and Column Wise

If the axis is not provided, this max() method will find the maximum element among all dimensions. However, we can compute the row and column-wise maximum values by specifying the axis value. For instance, in the below example, axis = 0 prints the maximum item in each column, and axis = 1 prints row-wise of the two-dimensional array.

The keepdims argument accepts boolean True or False, and the default value is false. Therefore, the result will reshape if you use the keepdims argument True value.

Here, the c variable finds the maximum in each column, and the d variable finds each row.

import numpy as np a = np.random.randint(20, 50, size = (5, 4)) print(a) b = np.max(a) print(b) c = np.max(a, axis = 0) print(c) d = np.max(a, axis = 1) print(d) e = np.max(a, axis = 1, keepdims = True) print(e)

```
[[39 31 22 44]
[38 25 31 23]
[29 48 33 26]
[23 35 43 33]
[32 46 24 21]]
48
[39 48 43 44]
[44 38 48 43 46]
[[44]
[38]
[48]
[43]
[46]]
```

### out argument

Use the out variable to save the max() method result in an array. Remember, the variable size has to match the output. In this example, np.max(a, axis = 1, out = x) will save the maximum of each row result in x array.

import numpy as np a = np.random.randint(10, 80, size = (5, 4)) print(a) x = np.arange(5) b = np.max(a, axis = 1) print(b) np.max(a, axis = 1, out = x) print('x = ', x)

```
[[47 14 73 75]
[35 60 64 79]
[54 69 48 28]
[65 44 66 43]
[19 76 47 60]]
[75 79 69 66 76]
x = [75 79 69 66 76]
```

### where argument

To use the where argument, you have to use the initial; otherwise, the Python numpy max will throw an error. For example, we used the where argument along the axis in the below code.

import numpy as np a = np.array([[2, 5, 3], [4, 6, 7], [1, 2, 9]]) print(a) b = np.max(a) print(b) c = np.max(a, where = [True, True, False], initial = 5) print(c) d = np.max(a, where = [False, True, True], axis = 0, initial = -7) print(d)

```
[[2 5 3]
[4 6 7]
[1 2 9]]
9
6
[-7 6 9]
```

where = [True, True, False], initial = 5 – It will omit the 3rd column and check the 1 and 2 columns to find the maximum value. Otherwise, the output = 9.

where = [False, True, True], axis = 0, initial = -7 – Because the first column is False, the maximum value becomes -7 (taken from the initial argument). Suppose you change the initial value to 8 for the second column. In that case, it compares the initial value (8) with column values (5, 6, and 2) and returns 8 as the output. For the 3rd column, the initial value is less than 9.