# Tutorial – Numpy Argmax, Numpy Argmin, Numpy Max, Numpy Min in Python While working on machine learning or data science projects, you might be required to find the maximum or minimum value or indices in the numpy array. In this post, we will see built-in numpy functions – numpy argmax, numpy argmin, numpy max and numpy min that can help us for this purpose.

Before we proceed further let us first import the numpy library.

### Importing Numpy

In :
```import numpy as np
```

## Numpy Argmax : numpy.argmax()

Numpy argmax returns the indices of the maximum value along the axis of a numpy array.

### Syntax

numpy.argmax(a, axis=None, out=None)

a – It is an input array.

axis (optional) – It is the index along which the indices of maximum values have to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have the appropriate shape and dtype.

### Examples:

Let us first create a sample array first –

In :
```arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
```
Out:
```[[ 7 14 57 22]
[22 25 5 26]]
```

Let us now find the maximum value from the entire array. The maximum value is 57 and its position is 2.

In :
```np.argmax(arr)
```
Out:
`2`

Let us find the indices of maximum value along each of the 4 columns.

In :
```np.argmax(arr,axis = 0)
```
Out:
`array([1, 1, 0, 1], dtype=int64)`

Let us find the indices of maximum value along each of the 2 rows.

In :
```np.argmax(arr,axis = 1)
```
Out:
`array([2, 3], dtype=int64)`

## Numpy Argmin : numpy.argmin()

Numpy argmin returns the indices of the minimum value along the axis of a numpy array.

### Syntax

numpy.argmin(a, axis=None, out=None)

a – It is an input array.

axis (optional) – It is the index along which the indices of minimum values have to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have appropriate shape and dtype.

### Examples:

Let us first create a sample array first –

In :
```arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
```
Out:
```[[ 7 14 57 22]
[22 25 5 26]]
```

Let us now find the maximum value from the entire array. The minimum value is 5 and its position is 6.

In :
```np.argmin(arr)
```
Out:
`6`

Let us find the indices of minimum value along each of the 4 columns.

In :
```np.argmin(arr,axis = 0)
```
Out:
`array([0, 0, 1, 0], dtype=int64)`

Let us find the indices of minimum value along each of the 2 rows.

In :
```np.argmin(arr,axis = 1)
```
Out:
`array([0, 2], dtype=int64)`

## Numpy Max : numpy.max()

Numpy max returns the maximum value along the axis of a numpy array.

### Syntax

numpy.max(a, axis=None, out=None, keepdims, initial, where)

a – It is an input array.

axis (optional) – It is the index along which the maximum values have to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis. If this is a tuple of ints, then the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have appropriate shape and dtype.

keepdims (optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. The input type is of bool.

initial (optional) – This specifies the minimum value of an output element. Must be present to allow computation on the empty slice. The input is scalar.

where (optional) – This signifies the elements to compare for the maximum. The input is array_like of bool.

### Examples:

Let us first create a sample array first –

In :
```arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
```
Out:
```[[ 7 14 57 22]
[22 25 5 26]]
```

Let us now find the maximum value from the entire array. The maximum value is 57.

In :
```np.max(arr)
```
Out:
`57`

Let us find the maximum value along each of the 4 columns.

In :
```np.max(arr, axis=0)
```
Out:
`array([22, 25, 57, 26])`

Let us find the maximum value along each of the 2 rows.

In :
```np.max(arr, axis=1)
```
Out:
`array([57, 26])`

## Numpy Min : numpy.min()

Numpy min returns the minimum value along the axis of a numpy array.

### Syntax

numpy.min(a, axis=None, out=None, keepdims, initial, where)

a – It is an input array.

axis (optional) – It is the index along which the minimum value has to be determined. The input is of type int. By default, the index is into the flattened array, else along the specified axis. If this is a tuple of ints, then the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

out (optional) – It specifies the array into which the output should be inserted. The size of this array should have the appropriate shape and dtype.

keepdims (optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. The input type is of bool.

initial (optional) – This specifies the minimum value of an output element. Must be present to allow computation on the empty slice. The input is scalar.

where (optional) – This signifies the elements to compare for the maximum. The input is array_like of bool.

### Examples:

Let us first create a sample array first –

In :
```arr = np.array([[7,14,57,22] ,[22,25,5,26]])
print(arr)
```
Out:
```[[ 7 14 57 22]
[22 25 5 26]]
```

Let us now find the maximum value from the entire array. The maximum value is 5.

In :
```np.min(arr)
```
Out:
`5`

Let us find the minimum value along each of the 4 columns.

In :
```np.min(arr, axis=0)
```
Out:
`array([ 7, 14, 5, 22])`

Let us find the maximum value along each of the 2 rows.

In :
```np.min(arr, axis=1)
```
Out:
`array([7, 5])`

## Conclusion

I hope it was a good tutorial. People often get confused between these, so do remember that numpy argmax and numpy argmin return the indices of the maximum and minimum value from the array. On the other hand numpy max and numpy min return the maximum and minimum value from the array.

Reference-  https://numpy.org/doc/