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.

Contents

### Importing Numpy

```
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 –

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

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

```
np.argmax(arr)
```

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

```
np.argmax(arr,axis = 0)
```

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

```
np.argmax(arr,axis = 1)
```

**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 –

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

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

```
np.argmin(arr)
```

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

```
np.argmin(arr,axis = 0)
```

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

```
np.argmin(arr,axis = 1)
```

**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 –

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

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

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

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

```
np.max(arr, axis=0)
```

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

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

**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 –

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

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

```
np.min(arr)
```

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

```
np.min(arr, axis=0)
```

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

```
np.min(arr, axis=1)
```

## 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.

**Click Here To Download This Tutorial in Interactive Jupyter Notebook**

- Also Read – Python Numpy Array – A Gentle Introduction to beginners
- Also Read – Tutorial – numpy.arange() , numpy.linspace() , numpy.logspace() in Python
- Also Read – Complete Numpy Random Tutorial – Rand, Randn, Randint, Normal
- Also Read – Tutorial – Numpy Shape, Numpy Reshape and Numpy Transpose in Python

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

## Like and Comment section (Community Members)

## Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

## Comments