Overview
- The NumPy function greater() compares the elements of two NumPy array-like instances and returns the results as a Boolean array. If the input parameters are scalars then a Boolean value is returned as the result. In other words, the greater() function implements the '>' operator for element-wise comparision.
- Given two values A and B, if A is greater than B then result is True. The result is False otherwise.
Example 1 - Compare integers from two ndarrays using numpy.greater()
# Example Python program that compares
# the elements of two ndarray objects
# using the greater() function of NumPy module
import numpy
from random import randrange
# Function that fills a 3x3 ndarray with random
# values between 0 to 199
def populateNDArray(arr_in):
for x in range(0, 3):
for y in range(0, 3):
arr_in[x][y] = randrange(200)
# Create ndarray 1
arr1 = numpy.ndarray(shape = (3, 3), dtype = int)
# Create ndarray 2
arr2 = numpy.ndarray(shape = (3, 3), dtype = int)
# Fill both the ndarray objects
populateNDArray(arr1)
populateNDArray(arr2)
print("Array1:")
print(arr1)
print("Array2:")
print(arr2)
# Do a comparision of corresponding elements from both the arrays
# using the function numpy.greater() i.e, arr1 > arr2
greaterResults = numpy.greater(arr1, arr2)
print("Array1 > Array2:")
print(greaterResults)Output
Array1:
[[193 138 86]
[175 180 16]
[ 66 77 89]]
Array2:
[[ 26 151 31]
[ 72 180 178]
[143 85 46]]
Array1 > Array2:
[[ True False True]
[ True False False]
[False False True]]Example 2 - Compare alphabets(ASCII bytes) using numpy.greater()
The upper case "C" has an ASCII value of 67 and the lower case "e" has an ASCII value of 101. Hence, when upper-case "C" and lower-case "e" are compared, the [numpy.greater()](#Overview of numpy.greater() function) returns False for the element [0][0] in the results.
# Example Python program that compares
# the elements of two ndarray instances
# containing random alphabets using numpy.greater()
# function
import numpy
import string
import random
# Fill ndarray with random ascii letters
def fillChars(arr_in):
for x in range(0, 3):
for y in range(0, 3):
arr_in[x][y] = random.choice(string.ascii_letters)
# Create an ndarray of alphabets
alphabetArray1 = numpy.ndarray(shape = (3, 3), dtype=numpy.dtype('S1'))
# Create one more ndarray of alphabets
alphabetArray2 = numpy.ndarray(shape = (3, 3), dtype=numpy.dtype('S1'))
# Fill both the ndarray objects with ascii letters
fillChars(alphabetArray1)
fillChars(alphabetArray2)
print("Array1:")
print(alphabetArray1)
print("Array2:")
print(alphabetArray2)
gtResults = numpy.greater(alphabetArray1, alphabetArray2)
print("Array1 > Array2:")
print(gtResults)Output
Array1:
[[b'C' b'T' b't']
[b'x' b'H' b'm']
[b'k' b'V' b'B']]
Array2:
[[b'e' b'K' b'o']
[b'k' b'l' b'i']
[b'W' b'Y' b'M']]
Array1 > Array2:
[[False True True]
[ True False True]
[ True False False]]Example 3 - Compare two NumPy scalars using NumPy.greater()
# Example Python program that compares scalars
# using Numpy.greater() function
import numpy
val1 = 10000
val2 = numpy.inf
x = numpy.greater(val1, val2)
print(x)
# Compare negative infinity and a positive integer
val3 = -numpy.inf
x = numpy.greater(val1, val3)
print(x)
# Compare two negative integers
val4 = -1
val5 = -5
x = numpy.greater(val4, val5)
print(x)
# Compare two positive integers
val6 = 10
val7 = 55
x = numpy.greater(val6, val7)
print(x)Output
False
True
True
False