How numpy argmin works in Python? Best example

How numpy argmin works in Python? Best example
“`html

When working with numerical data in Python, particularly with arrays, finding the index of the smallest element can be incredibly useful. This is where numpy.argmin() comes into play. It’s a simple yet powerful function that allows us to locate the minimum value’s index within an array. Let’s dive into the details of how it works and explore some practical examples.

What is numpy.argmin()?

numpy.argmin() is a function in the NumPy library that returns the index of the minimum element in an array. If the array is multidimensional, you can specify the axis along which to find the minimum index.

Basic Usage of numpy.argmin()

The basic syntax of numpy.argmin() looks like this:

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

Here’s what each parameter means:

  • a: The input array.
  • axis (optional): Specifies the axis along which to find the minimum index. If not specified, it works on a flattened version of the array.
  • out (optional): If provided, stores the result in the given array.
  • keepdims (optional): Determines whether the reduced dimensions are kept (useful when working with multi-dimensional arrays).

Example 1: Finding the Index of the Minimum Value in a 1D Array

Let’s start with a basic example to find the index of the smallest number in a one-dimensional NumPy array:

import numpy as np

arr = np.array([10, 15, 2, 30, 5])

min_index = np.argmin(arr)

print(min_index)  # Output: 2

Here, the minimum value is 2, which is located at index 2.

Example 2: Using numpy.argmin() on a 2D Array

For multi-dimensional arrays, we can specify an axis to find the minimum value’s index along rows or columns.

arr_2d = np.array([[3, 7, 1], 
                   [8, 2, 9], 
                   [4, 6, 5]])

min_index_col = np.argmin(arr_2d, axis=0)  # Find index of min along columns
min_index_row = np.argmin(arr_2d, axis=1)  # Find index of min along rows

print(min_index_col)  # Output: [0 1 0]
print(min_index_row)  # Output: [2 1 2]

In this example:

  • axis=0 checks for the minimum values across columns.
  • axis=1 checks for the minimum values across rows.

Example 3: Working with Flattened Arrays

If no axis is specified, numpy.argmin() works on a flattened version of the array:

arr_2d = np.array([[3, 7, 1], 
                   [8, 2, 9], 
                   [4, 6, 5]])

flattened_min_index = np.argmin(arr_2d)

print(flattened_min_index)  # Output: 2

Here, the function treats the array as a single list and returns 2, corresponding to the minimum value 1.

Comparison: numpy.argmin() vs. numpy.min()

It’s easy to confuse numpy.argmin() with numpy.min(). Here’s how they differ:

Function Purpose Example Output
numpy.min() Returns the minimum value itself 1
numpy.argmin() Returns the index of the minimum value 2

Example 4: Using numpy.argmin() with Negative Values

Negative values are also handled properly by numpy.argmin():

arr = np.array([10, -4, 5, -20, 0])

min_index = np.argmin(arr)

print(min_index)  # Output: 3

Since -20 is the smallest value, its index 3 is returned.

Real-World Application of numpy.argmin()

The function is useful in various scenarios, including:

  1. Finding the coldest day in a temperature dataset.
  2. Identifying the best-performing algorithm by finding the lowest error in a set of results.
  3. Comparing stock prices and locating the point where the price was at its lowest.

Conclusion

Understanding how numpy.argmin() works in Python is essential when working with numerical data. Whether it’s a simple one-dimensional array or a complex multi-dimensional dataset, this function makes it easy to locate the minimum value’s position. By using the axis parameter, you can fine-tune your results to fit your specific needs.

“` Other interesting article: How numpy argmax works in Python? Best example