# Numpy argmin function Implementation with Steps

There are many functions in numpy that allows you to easily manipulate the numpy array. The numpy argmin() function is one of them. It allows you to find the index of the minimum value of the numpy array. In this tutorial you will know all the steps to implement the numpy argmin() function.

## Syntax of the numpy argmin() function

The syntax of the argmin() function is the below.

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

Explanation of the parameters are the below.

a: This is the input array.
axis: Axis or axes along which to operate. By default, it operates on the flattened array. If specified, it returns indices along the specified axis or axes.
out: This is an optional output array. If provided, the result is placed in this array. It must have the same shape as the expected output but of the type int.

## Steps to implement Numpy argmin Function

Below are the steps to implement numpy argmin function.

### Step 1: Import Numpy

Make sure you have NumPy installed and import it in your script or Jupyter Notebook using the import statment.

``import numpy as np``

### Step 2: Create an Array

Create the array on which you want to find the indices of the minimum values.

``````   my_array = np.array([[40, 80, 20],
[60, 10, 70],
[90, 30, 50]])``````

### Step 3: Use numpy.argmin

Apply the `numpy.argmin` function to find the indices of the minimum values. You can specify the axis along which to operate.

``   min_indices = np.argmin(my_array)``

If you want to find indices along a specific axis (e.g., columns or rows), you can specify the `axis` parameter.

``````   min_indices_axis_0 = np.argmin(my_array, axis=0)  # along columns
min_indices_axis_1 = np.argmin(my_array, axis=1)  # along rows``````

The result will be an array of indices corresponding to the minimum values along the specified axis.

Note: If you want to find the indices of the minimum values directly (not flattened), you can use the `unravel_index` function.

``   min_indices_unraveled = np.unravel_index(np.argmin(my_array), my_array.shape)``

This will give you a tuple of indices corresponding to the minimum value in the original array.

Below is the complete code for the above steps.

``````import numpy as np

# Create an array
my_array = np.array([[40, 80, 20],
[60, 10, 70],
[90, 30, 50]])

# Find the indices of the minimum values
min_indices = np.argmin(my_array)
min_indices_axis_0 = np.argmin(my_array, axis=0)
min_indices_axis_1 = np.argmin(my_array, axis=1)
min_indices_unraveled = np.unravel_index(np.argmin(my_array), my_array.shape)

print("Indices of the minimum value:", min_indices)
print("Indices of the minimum values along axis 0:", min_indices_axis_0)
print("Indices of the minimum values along axis 1:", min_indices_axis_1)
print("Unraveled indices of the minimum value:", min_indices_unraveled)``````

Output

``````Indices of the minimum value: 4
Indices of the minimum values along axis 0: [0 1 0]
Indices of the minimum values along axis 1: [2 1 1]
Unraveled indices of the minimum value: (1, 1)``````

### Conclusion

You can use the numpy.argmin() to find the index of the minium value of a numpy array. Use the above steps to find the index. You can also find the index of minimum value directly using the unravel_index function.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

## SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In: 