The less() function of NumPy module

Given two NumPy array-likes the less() function returns the result of element1 < element2, where element1, element2 are drawn from the two input array-likes in sequential order.

Logical Operations Updated 10 Nov 2024 1 min read

Overview

  • The less() function of NumPy module compares the elements of two NumPy array-likes and returns the results in a Boolean array. If element1 is less than element2 than the result is True; The result is False otherwise. If the input parameters are scalars than a Boolean value is returned.
  • The less() function does the opposite of the greater() function.

Example

The Python example uses random.uniform() to generate random floating point numbers between the given range of 5.1 and 9.1 and fills the ndarray objects.

python
# Example Python program that compares the elements

            # of two ndarray instances using numpy.less() function

            import numpy

            import random

# Function that fills a 3x3 ndarray with random

            # floating point numbers between 5.1 and 9.1

            def fillFloats(arr_in):

                for x in range(0, 3):

                    for y in range(0, 3):

                            arr_in[x][y] = random.uniform(5.1, 9.1)

# Create 3x3 ndarray to store floating point numbers

            fArray1 = numpy.ndarray(shape = (3, 3), dtype = float)

# Create 3x3 ndarray to store floating point numbers

            fArray2 = numpy.ndarray(shape = (3, 3), dtype = float)

# Fill the arrays

            fillFloats(fArray1)

            fillFloats(fArray2)

print("Array1:")

            print(fArray1)

print("Array2:")

            print(fArray2)

# Compare using numpy.less() function

            compareResults = numpy.less(fArray1, fArray2)

print("Array1 < Array2:")

            print(compareResults)

Output

Output
Array1:

[[7.01322589 7.75920796 6.29760756]

 [6.36789148 7.43626891 6.91623009]

 [7.03070352 6.74934007 6.1260513 ]]

Array2:

[[5.76370196 8.00060042 6.89754765]

 [8.54632546 8.67683287 6.35581307]

 [7.50265029 6.64350998 6.67909488]]

Array1 < Array2:

[[False  True  True]

 [ True  True False]

 [ True False  True]]