This tutorial explains how to use the Numpy argmin function. It explains the syntax of np.argmin, and also shows clear examples.

If you need something specific, you can click on any of the links below. These links will take you to the appropriate section of the tutorial.

**Table of Contents:**

If you really want to understand how Numpy argmin works though, you should probably read the whole tutorial. It will make more sense if you read the whole tutorial.

## A quick introduction to Numpy Argmin

Let’s start with a quick introduction to the argmin function.

Numpy argmin function (much like it’s companion function, Numpy argmax) frequently confuses new Numpy users. However, it starts to make sense once you see a clear explanation and clear examples.

In its simplest use, the argmin function returns the *index* of the minimum value of a Numpy array.

So it’s similar to the Numpy minimum function, but instead of returning the minimum value, it returns the *index* of the minimum value.

To make this a little more clear, I’m going to quickly review some basics about Numpy and Python. Knowing these basics should make argmin easier to understand.

### Numpy arrays store data

For starters, let’s review what a Numpy arrays are.

Numpy arrays store numeric data in a grid format.

So for example, in the Numpy array above, we have the values `91`

, `92`

, `93`

, `7`

, and `95`

, arranged in a one dimensional array.

So Numpy arrays are a special data structure that store numeric data in a grid-like format.

### Locations in a Numpy array have an “index”

Next, let’s review Numpy indexes. Indexes are important for the argmin function.

All Numpy arrays have indexes. The values of the index are like numeric addresses for each position in the array.

1-dimensional arrays are simpler to understand than multi-dimensional arrays, so let’s look at a 1D example.

If we have a 1D Numpy array, every location in that array has an index. Again, the index is sort of like a numeric address for each position in the array.

If you’re familiar with lists, tuples, and other Python data structures, this probably sounds familiar. Most collections and sequences in Python have indexes, just like these Numpy array indexes.

And just like the indexes for lists and tuples, indexes for Numpy arrays start at 0.

### Numpy Argmin Identifies the Minimum Value and Returns the Associated Index

Now, let’s bring this back to the argmin function.

When we call `np.argmin()`

, the argmin function identifies the minimum value in the array.

But instead of retrieving the minimum value itself, argmin retrieves the *index* that’s associated with the minimum value.

That’s really all it does! The np.argmin function simply returns the index of the minimum value in the array.

Having said that, there are more complicated ways of using the np.argmin. For example, you can use the function along specific axes. I’ll show you examples of how to do that in the examples section.

But before we look at some examples, let’s look at the syntax.

## The syntax of np.argmin

Ok. Let’s take a look at the syntax.

#### A quick note

One reminder before we look at the syntax

The syntax explanation and the examples below assume that you’ve imported Numpy with the alias ‘`np`

‘.

You can do that with this code:

import numpy as np

Among Python data scientists, this is the common convention for importing Numpy. So, we’ll be using this convention in the following syntax explanation and examples.

With that said, let’s look at the syntax.

### np.argmin syntax

The syntax of np.argmin is fairly simple.

You call the function as `np.argmin()`

.

Inside the parenthesis, you have a few parameters that you can use to control how the function works.

Let’s look at those parameters.

### The parameters of np.argmin

The np.argmin function really only has 3 parameters:

`a`

`axis`

`out`

The `out`

parameter is somewhat rarely used, so we’re not going to discuss it here.

But let’s look at the `a`

parameter and `axis`

parameter.

`a`

(required)

The `a`

parameter enables you to specify the input array that you want to operate on.

So if you want to operate on an array called `myarray`

, you can call the function as `np.argmin(a = myarray)`

.

Keep in mind that you need to provide an argument to this parameter.

Having said that, you don’t need to explicitly use this parameter. Instead, you can pass in an argument by position like this: `np.argmin(myarray)`

. The argmin function will assume that the first argument to the function is the input array to be passed to the `a=`

parameter.

Also note that this parameter will accept many data structures as arguments. Typically, we’ll pass in a Numpy array as the argument, but the np.argmin function will also accept “array like” objects, such as Python lists.

`axis`

The `axis`

parameter enables you to control the axis along which to use argmin.

Remember: Numpy arrays have *axes*. Axes are like directions along the numpy array.

Keep in mind, that the `axis`

parameter is optional. If you use it, `np.argmin`

will retrieve the index values for the minima along particular axes. But if you don’t use it, then argmin will flatten out the array and retrieve the index of the minimum of the flattened array.

To be honest, how axes work is little difficult to understand without examples. So I’ll show you some examples in the examples section bellow. I also strongly recommend that you read our tutorial that explains Numpy axes.

## Examples of how to use Numpy Argmin

Ok. Let’s take a look at a few examples.

Things almost always make more sense when you can look at some examples, but that’s particularly true with np.argmin.

**Examples:**

- Use argmin on a 1-dimensional array
- Apply argmin to a 2-dimensional array
- Use argmin along axis 0
- Use argmin along axis 1

#### Run this code first

Before you run any of the examples, you need to import Numpy.

You can do that with the following code:

import numpy as np

When we do this, we’ll be able to call our Numpy functions starting with the alias ‘`np`

‘.

### EXAMPLE 1: Use argmin on a 1-dimensional array

Ok, let’s start simple.

Here, we’re going to identify the index of the minimum value of a 1-dimensional Numpy array.

First, let’s just create the array:

my_1d_array = np.array([91,92,93,7,95])

Now, let’s use np.argmin.

np.argmin(a = my_1d_array)

OUT:

3

###### Explanation

This is a simple example.

This is a 1D array, with several elements. The minimum value of the array is 7.

The minimum value, 7, is at index position 3, so argmin returns the value ‘3’.

### EXAMPLE 2: Apply argmin to a 2-dimensional array

Next, let’s look at how argmin works on a 2-dimensional array.

First, we need to create our array with the Numpy array() function.

my_2d_array = np.array([[8,92,93],[94,9,7]])

Notice that there are some high values and some low values.

Now that we have our 2D array, let’s apply np.argmin.

np.argmin(my_2d_array)

OUT:

5

###### Explanation

The output for this example is 5. Why?

In this example, we’re operating on a 2-dimensional array.

But, by default, if we’re use np.argmin on a 2-dimensional array and we do *not* specify an axis, the Numpy argmin function applies a 2-step process.

First, np.argmin flattens the 2-dimensional array to a 1-dimensional array. (Keep in mind, np.argmin will also flatten out higher dimensional arrays).

Second, np.argmin operates on the new flattened array.

When we flatten out the array in this example, the minimum value, 7, is at index position 5 of the flattened array.

Therefore, numpy.argmin returns 5.

### EXAMPLE 3: Use argmin along axis 0

Next, let’s apply argmin to a 2-dimensional array, and also use the `axis`

parameter.

In this example, we’ll set `axis = 0`

.

We’ll re-use the array that we created in example 2, but if you didn’t run that code already, here’s the code to create our 2D array:

my_2d_array = np.array([[8,92,93],[94,9,7]])

Now that we have our array, let’s use Numpy argmin with `axis = 0`

:

np.argmin(my_2d_array, axis = 0)

OUT:

array([0, 1, 1])

###### Explanation

This example is harder to understand, so let’s break it down.

Remember what I said earlier: Numpy axes are like directions along a Numpy array. For a 2-dimensional array, the axis-0 direction points downward.

When we use argmin in the axis-0 direction, the function identifies the minimum along that axis and returns the index.

Since this example is a little more complicated, let’s look carefully.

When we set `axis = 0`

, we’re using argmin in the axis-0 direction. Remember, for a 2D array, axis-0 points downward.

Also remember that for a 2D array, every row has an index. So you can see “row 0” and “row 1”. (Again, all Python indexes start at 0, so the “first” row is actually the 0th row.)

From there, argmin simply looks for the minimum value along the axis-0 direction. When it finds the minimum value, argmin returns the row index.

So 0 is the minimum value in the first column, and the *row index* of that value is 0.

The minimum value in the second column is 9, which is in row 1.

Similarly, the minimum value in the third column is 7, which is also in row 1.

np.argmin outputs the indexes of the minimum values in the axis-0 direction. So the output is `[0, 1, 1]`

.

Effectively, when we set `axis = 0`

, it’s like applying argmin along the columns.

### EXAMPLE 4: Use argmin along axis 1

Ok. Let’s do one more example.

Let’s apply argmin in the axis-1 direction.

First, let’s create our array (the same array as the previous two examples):

my_2d_array = np.array([[8,92,93],[94,9,7]])

And now, let’s use argmin.

np.argmin(my_2d_array, axis = 1)

OUT:

array([0, 2])

###### Explanation

This example is also a hard to understand. To understand it, you really need to know how axes work for Numpy arrays.

In this example, we’re applying np.argmin along axis-1. Remember: for 2D Numpy arrays, axis-1 points *horizontally*.

When we set `axis = 1`

, argmin identifies the minimum value for every row. Then it returns the column index of each minimum value.

So for the first row, the minimum value is 8. That value has a column index of 0.

For the second row, the minimum value is 7. That value has a column index of 2.

So np.argmin outputs the column indexes of the minimum values: `[0,2]`

.

### If you’re still confused, read more about indexes and axes

I’ve tried to make these examples as clear as possible, but I realize that Numpy argmin is difficult to understand.

Ultimately, to understand Numpy argmin, you need to understand Numpy indexes. You also need to understand how Numpy axes work. Those things being said, if you haven’t already, you should read our tutorial on Numpy axes.

## Leave Your Questions in the Comments Section

Do you have other questions about Numpy argmin? Is there something I’ve missed?

If so, just leave your questions in the comments section near the bottom of the page.

## Join our course to learn more about Numpy

In this tutorial, I’ve shown you how to use Numpy argmin.

Numpy argmin is useful for some very specific things, but if you’re working with numeric data in Python, there’s a lot more to learn. To use Numpy properly, you’ll need to know many other Numpy functions.

So if you’re serious about learning Numpy, you should consider joining our premium course called *Numpy Mastery*.

Numpy Mastery will teach you everything you need to know about Numpy, including:

- How to create Numpy arrays
- How to Numpy axes work
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to reshape, split, and combine your Numpy arrays
- Applying mathematical operations on Numpy arrays
- and more …

The course will also show you our unique practice system. This practice system will enable you to *memorize* all of the Numpy syntax that you learn.

If you’re struggled to remember Numpy syntax, this is the course you’ve been looking for.

If you practice like we show you, you’ll memorize all of the critical Numpy syntax in only a few weeks.

Find out more here:

Fantastic as always. It would be better if there is a search button on the website so that anyone can search it out for anything like “Numpy”, once it entered; all the relevant blogs will get loaded for Numpy

Thank you very much.

Excellent and Excellent.