Numpy greater_equal: How to Use np greater_equal()

To compare two arrays in Numpy, use the np.greater_equal() method. It checks whether each element of one array is greater than or equal to its corresponding element in the second array or not. The greater_equal() method returns boolean values in Python.

Numpy greater_equal()

Numpy greater_equal() method is used to compare two arrays element-wise to check whether each element of one array is greater than or equal to its corresponding element in the second array or not. The greater_equal() method returns bool or a ndarray of the bool type.

Syntax

numpy.greater_equal(arr1, arr2)

Parameters

The ndarrays arr1 and arr2 such that element-wise comparison takes place between corresponding elements of these n-dimensional arrays.

Return Value

A ndarray of bool type returning True if element x1 of arr1 is greater than or equal to corresponding element x2 of arr2. Returns False otherwise.

 

Numpy greater_equal

Note

If shapes of both the arrays are not equal, then they must be broadcastable to a standard shape.

Examples

The following code demonstrates the use of the greater_equal() method.

import numpy as np

x = np.array([11, 5])
y = np.array([2, 6])

print(np.greater_equal(x, y))

Output

[ True False]

Example 2

The following example demonstrates the case where ndarrays are of different shapes.

import numpy as np

x = np.array([11, 5])
y = np.array([[2, 6], [13, 13]])

print(x.shape)
print(y.shape)

print(x.shape == y.shape)

print(np.greater_equal(x, y))

Output

(2,)
(2, 2)
False
[[ True False]
 [False False]]

Example 3

The following example demonstrates the case where float and int values are compared.

import numpy as np

x = np.array([11, 5])
y = np.array([11.1, 4.99])
print(np.greater_equal(x, y))

Output

[False  True]

Example 4

The following example demonstrates the case where a complex number is compared.

import numpy as np

x = np.array([2, 5j])
y = np.array([2, 5])
print(np.greater_equal(x, y))

Output

[ True False]

Example 5

The following example demonstrates the application of this method in a simple programming context.

Given the passing marks for students studying in different schools in an array and the marks scored by these students in another array, print which of these students have passed. Print True for a pass, False otherwise.

See the following code.

import numpy as np

arr1 = []
arr2 = []

n = int(input("Number of students: "))

for i in range(n):
    arr1.append(float(input()))
    arr2.append(float(input()))

arr1 = np.array(arr1)
arr2 = np.array(arr2)

print(np.greater_equal(arr2, arr1))

Output

Test Case 1:
->python3 example5.py
Number of students: 2
1.1
1.2
2.4
2.0
[ True False]

Test Case 2:
Number of students: 3
35
36
70
50
40
100
[ True False  True]

That is it for the Numpy greater_equal() method. Thanks for taking it.

Conclusion

To create an element-wise comparison (greater, greater_equal, less, and less_equal) of two given arrays, we can use the Numpy greater_equal() method.

See also

Numpy prod()

Numpy square()

Numpy divide()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.