Numpy stands for Numerical Python. With numpy, we can perform mathematical computations at high speed in python. The Numpy library in python consists of a large collection of high-level mathematical functions. These functions are used for handling large, multi-dimensional arrays and matrices in python and for performing various logical and statistical operations on them. Out of the many available functions in numpy, we shall be looking into one such function – **Numpy Argmin**.

## What is numpy argmin?

**Numpy argmin is a function in python which returns the index of the minimum element from a given array along the given axis. The function takes an array as the input and outputs the index of the minimum element. **

**The input array can be a single-dimensional array as well as a multi-dimensional array. We can also perform sorting on the array using this argmin function. **

## Syntax of Numpy argmin

**The syntax of the numpy argmin function is:**

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

**The function accepts three parameters – a, axis and out.**

## Parameters of Numpy argmin:

**a**: It is the array like input given to the function. It is the array for which the index of the minimum element has to be found.

**axis**: It is an optional parameter which takes an integer value. If not specified, then it is None by default. If specified, then the minimum value’s index is returned along that axis.

**out**: This is also an optional parameter. If it is provided, then the result of the argmin function will be stored into this array. But, the given array’s size should be compatible with the return array’s shape.

## Return Value of Numpy argmin:

**index_array**: It is an array containing integer values. These integer values contain the index(s) of the minimum value along the given axis.

## Numpy argmin() for 1D array

Let us understand with some python code how to use the *argmin()* function. First, we shall import the numpy library.

```
import numpy as np
```

Now, we shall take a numpy array and store it into a variable named *‘array’*.

```
array = np.array([10,7,2,1,5,8,11,9])
```

After creating the array, we shall pass the* ‘array’* as an argument to the* argmin() *function. Then, we shall print the return value of that function.

```
print(np.argmin(array))
```

**The output is:**

3

The minimum value in the array is 1. Since the indexing starts from 0, the index of 1 – which is 3 – shall be returned by the function.

### The Entire Code is:

```
import numpy as np
array = np.array([10,7,2,1,5,8,11,9])
print(np.argmin(array))
```

## Argmin() for multi dimensional array

We can also use the argmin function to calculate the minimum value for a multi dimensional array. The output will be calculate along the axis of the multi dimensional array.

**Let us take a two dimensional array first.**

```
import numpy as np
array = np.array([[10,7,2,1],[5,8,11,9]])
```

Now, we shall pass the array to the argmin function. We will not be mentioning the* ‘axis’ *parameter explicitly.

**So, axis will have its default value – ‘None’.**

```
print(np.argmin(array))
```

Since the* ‘axis’* is *‘None’,* the output will be the index of the element in the form of a flattened array.

**Output:**

3

The answer is similar to that obtained in case of the one dimensional array.

### The Entire Code is:

```
import numpy as np
array = np.array([[10,7,2,1],[5,8,11,9]])
print(np.argmin(array))
```

## Numpy Argmin with different axis values

Now, we shall try to change the axis values of the argmin function and see how it changes the output.

Earlier, we were using *axis = None*. Now we shall first try for *axis = 0*. We shall take the same two dimensional array as before.

**Axis = 0**:

```
import numpy as np
array = np.array([[10,7,2,1],[5,8,11,9]])
```

Now, in the argmin function, we shall pass* ‘axis = 0’* as the second argument.

```
print(np.argmin(array, axis = 0))
```

The output will contain an array with the index value of the minimum element for each column of the 2D array.

[1 0 0 0]

In the first column, between 10 and 5, 5 is minimum. So, the index value of 5,which is 1, shall be passed. Similarly the index value for the other elements shall be passed too.

### The Entire Code is:

```
import numpy as np
array = np.array([[10,7,2,1],[5,8,11,9]])
print(np.argmin(array, axis = 0))
```

**Axis = 1:**

Now, we shall pass* axis = 1* instead of *axis = 0.*

```
import numpy as np
array = np.array([[10,7,2,1],[5,8,11,9]])
print(np.argmin(array, axis = 1))
```

The output will be an array containing indexes of minimum elements along the row of the two dimensional array.

[3 0]

So, in the first row, the minimum element is at position 3 and in the second row, minimum element is at position 0.

## Numpy argmin with Condition

We can also apply *argmin() *function for a given array where the elements fulfill a certain condition. For this, we shall be making use of Masked Arrays in numpy. Masked Arrays are those arrays where the array elements may be invalid entries.

For all the elements which do not fulfill the given condition, we shall mask them and not consider them. Then for the rest of the elements, the index of the minimum value shall be returned.

Here, we shall only include those elements whose value is greater than or equal to 6. So in the MaskedArray, we shall pass the opposite condition, i.e., the elements whose value is less than 6 in order to mask those elements.

```
import numpy as np
array = np.array([10,7,2,1,5,8,11,9])
array = np.ma.MaskedArray(array, array<6)
print(array)
```

**The printed elements would look something like this:**

[10 7 -- -- -- 8 11 9]

**Now, we shall print the np.argmin() function’s return value.**

```
print(np.argmin(array))
```

Since out of the unmasked elements 7 has the minimum value, its index 1 would be printed.

1

**The Entire Code is:**

```
import numpy as np
array = np.array([10,7,2,1,5,8,11,9])
array = np.ma.MaskedArray(array, array<6)
print(array)
print(np.argmin(array))
```

## Numpy Argmin with Matrix

We can also use the argmin function to find an index of minimum elements from a matrix. For that, we will access it using the

**Syntax:**

numpy.matrix.argmin

We shall create a matrix using numpy’s matrix function.

```
import numpy as np
matrix = np.matrix(np.arange(16).reshape((4,4)))
print(matrix)
print(matrix.argmin())
```

The output is:

[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 0

Here, 0 is printed as the index of the minimum value 0.

## Also, Read

## Numpy argmin FAQ

Here are a few questions we hear often when talking about Numpy argmin.

**How to handle nan values while using numpy array?**

We can mask the nan values present in a given array using the MaskedArray. All the nan values will be masked and not considered for the argmin function.**The code is :***import numpy as nparray = np.array([10, np.nan,2, np.nan,5,8, np.nan,9])array = np.ma.MaskedArray(array, np.isnan(array))print(array)print(np.argmin(array))*

**The output is:**

*[10.0 — 2.0 — 5.0 8.0 — 9.0]*

2

2

This sums up everything about Numpy Argmin. If you have any questions, let us know in the comments below.

*Until next time, Keep Learning!*