Python Numpy argmin() - Find Minimum Index

Updated on November 8, 2024
argmin() header image

Introduction

The numpy.argmin() function in Python is a tool for finding the index of the minimum value in an array. This function is integral to data analysis, machine learning preprocessing, and anywhere you need to identify the position of the smallest element quickly.

In this article, you will learn how to effectively utilize the numpy.argmin() function in your Python code. Explore practical examples to see how this function operates on various data structures such as single-dimensional and multi-dimensional arrays.

Understanding numpy.argmin()

Basic Usage in Single-dimensional Arrays

  1. Import the numpy library.

  2. Create a single-dimensional numpy array.

  3. Apply the argmin() function to find the index of the minimum value.

    python
    import numpy as np
    
    array = np.array([10, 20, 5, 40])
    min_index = np.argmin(array)
    print(min_index)
    

    This code returns the index 2 because 5 is the minimum value in the array, and it is located at the index 2.

Handling Multi-dimensional Arrays

  1. Initialize a two-dimensional array.

  2. Use argmin() to find the index of the minimum value across the entire array.

  3. Further, apply argmin() along a specific axis to find the indices of minimum values in that dimension.

    python
    multi_array = np.array([[10, 2], [5, 20]])
    overall_min = np.argmin(multi_array)
    column_min = np.argmin(multi_array, axis=0)
    row_min = np.argmin(multi_array, axis=1)
    
    print("Index of overall minimum:", overall_min)
    print("Indices of minimums in each column:", column_min)
    print("Indices of minimums in each row:", row_min)
    

    In this snippet:

    • The overall minimum value is 2, and argmin() returns 1 as it is located at the second position if the array is flattened.
    • The column-wise minima are 5 and 2, with indices 1 and 0 respectively, in their columns.
    • The row-wise minima are 2 and 5, located at indices 1 and 0 in their respective rows.

Using argmin() with Conditioned Data

  1. Establish the numpy array with varied data.

  2. Apply a conditional operation to generate a boolean array.

  3. Use argmin() to determine the first True position in the conditioned array.

    python
    data_array = np.array([10, -20, -5, 50, -60])
    negative_cond = data_array < 0
    first_negative_index = np.argmin(negative_cond == False)
    
    print("First index with a negative value:", first_negative_index)
    

    Here, argmin() identifies the first index where the condition (data_array < 0) becomes True, thus pinpointing the first negative value in the data set.

Conclusion

The numpy.argmin() function is a versatile tool for efficiently locating the index of minimum values within an array, providing valuable functionality across single and multi-dimensional data sets. Whether handling simple arrays or complex matrices, understanding how to leverage argmin() enhances data analysis and preprocessing tasks effectively. Utilize these strategies to simplify searches for minimum values in your datasets, improving both performance and readability of your Python code.