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 # Function that fills a 3x3 ndarray with random # Create ndarray 1 # Create ndarray 2 # Fill both the ndarray objects # Do a comparision of corresponding elements from both the arrays |
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() returns False for the element [0][0] in the results.
# Example Python program that compares # Fill ndarray with random ascii letters # Create an ndarray of alphabets # Create one more ndarray of alphabets # Fill both the ndarray objects with ascii letters gtResults = numpy.greater(alphabetArray1, alphabetArray2) |
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 val1 = 10000 x = numpy.greater(val1, val2) # Compare negative infinity and a positive integer # Compare two negative integers # Compare two positive integers |
Output:
False True True False |