Python Numpy minimum() - Find Minimum Value

Updated on November 18, 2024
minimum() header image

Introduction

The minimum() function in NumPy is a versatile tool designed to find the smallest element between two arrays or within an array on an element-wise basis. NumPy, a fundamental package for numerical computation in Python, provides this function to handle large arrays efficiently and with ease. The minimum() function simplifies the process of comparing elements, making it invaluable in data analysis, scientific computing, and various applications requiring minimum value computation.

In this article, you will learn how to use the NumPy minimum() function to effectively find minimum values in single or multiple arrays. Explore practical examples that demonstrate how to apply this function in different scenarios and understand its behavior with various data types.

Using minimum() with Arrays

Finding Minimum Between Two Arrays

  1. Import the NumPy package.

  2. Define two arrays.

  3. Apply the minimum() function to these arrays.

    python
    import numpy as np
    
    a = np.array([5, 7, 9])
    b = np.array([3, 6, 8])
    min_result = np.minimum(a, b)
    print(min_result)
    

    This example outputs the minimum values between corresponding elements in arrays a and b, resulting in [3, 6, 8].

Finding the Minimum with Scalar Values

  1. Combine scalar values and arrays to find minimum values.

  2. Use a single array and a scalar to apply the function.

    python
    import numpy as np
    
    c = np.array([10, 20, 30])
    scalar = 15
    min_scalar_result = np.minimum(c, scalar)
    print(min_scalar_result)
    

    In this snippet, the minimum() function compares each element in array c with the scalar value 15, resulting in an output of [10, 15, 15].

Using minimum() in Advanced Scenarios

Minimum Values in Multi-dimensional Arrays

  1. Create a multi-dimensional array.

  2. Use minimum() along a specified axis.

    python
    import numpy as np
    
    d = np.array([[1, 2, 3], [4, 5, 6]])
    e = np.array([[3, 2, 1], [0, 0, 0]])
    min_multi_dim = np.minimum(d, e)
    print(min_multi_dim)
    

    Here, the function computes the minimum for each corresponding pair in the multi-dimensional arrays d and e.

Handling NaN Values

  1. Include NaN values in your arrays.

  2. Observe the behavior of minimum() with NaN.

    python
    import numpy as np
    
    f = np.array([np.nan, 2, np.nan])
    g = np.array([1, np.nan, 3])
    min_nan = np.minimum(f, g)
    print(min_nan)
    

    Since NaN interacts peculiarly when comparing with numeric values, the result shows NaN where any of the compared values is NaN.

Conclusion

The NumPy minimum() function is a powerful tool to compute minimum values across arrays or between array elements in a comprehensive, efficient manner. Familiarizing yourself with this function allows for detailed and efficient data analysis and manipulation. Whether you're dealing with simple or complex datasets, the minimum() function extends your capabilities in handling comparisons optimally within your Python environments. Embrace these techniques to simplify and accelerate your analytical tasks by efficiently finding minimum values in your data.