Python numpy Comparison Operators

The Python numpy comparison operators and functions used to compare the array items and returns Boolean True or false. The Python Numpy comparison functions are greater, greater_equal, less, less_equal, equal, and not_equal. Like any other, Python Numpy comparison operators are <, <=, >, >=, == and !=

To demonstrate these Python numpy comparison operators and functions, we used the numpy random randint function to generate random two dimensional and three-dimensional integer arrays.

The first array generates a two-dimensional array of size 5 rows and 8 columns, and the values are between 10 and 50.

arr1 = np.random.randint(10, 50, size = (5, 8))

This second array generates a random three-dimensional array of size 2 * 3 * 6. The generated random values are between 1 and 20.

arr2 = np.random.randint(1, 20, size = (2, 3, 6))

Python numpy Array greater

It is a simple Python Numpy Comparison Operators example to demonstrate the Python Numpy greater function. First, we declared an array of random elements. Next, we are checking whether the elements in an array are greater than 0, greater than 1 and 2. If True, True returned otherwise, False returned. 

# Python array greater
 
import numpy as np

x = np.array([0, 2, 3, 0, 1, 6, 5, 2])
print('Original Array = ', x)
print('\nGreater Than 0 = ', np.greater(x, 0))
print()
 
print('Greater Than 1 = ', np.greater(x, 1))
print()
 
print('Greater Than 2 = ', np.greater(x, 2))
Python Numpy Array greater function 1

In this example, we used the Python Numpy greater function on 2D and 3D Arrays. The first array greater function checks whether the values in two dimensional are greater than 30 or not. If true, then Boolean True returned otherwise false returned. Next, we are checking whether the Python array elements in a three-dimensional array are greater than 10 or not.

import numpy as np
 
arr1 = np.random.randint(10, 50, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print()
print(np.greater(arr1, 30))
 
arr2 = np.random.randint(1, 20, size = (2, 3, 6))
print('\n-----Three Dimensional Random Array----')
print(arr2)
print()
print(np.greater(arr2, 10))
Python Numpy Array greater function 2

Python Numpy Array greater_equal

The Python Numpy greater_equal function checks whether the elements in a given array (first argument) is greater than or equal to a specified number(second argument). If True, True returned otherwise, False.

The first Numpy statement checks whether items in the area is greater than or equal to 2. The second statement checks the items in a random 2D array is greater than or equal to 25. The third statement checks randomly generated three-dimensional array items that are greater than or equal to 7.

import numpy as np
 
arr = np.array([0, 2, 3, 0, 1, 6, 5, 2])
print('Original Array = ', arr)
print('Greater Than or Equal to 2 = ', np.greater_equal(arr, 2))
 
arr1 = np.random.randint(10, 50, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print()
print(np.greater_equal(arr1, 25))
 
arr2 = np.random.randint(1, 15, size = (2, 3, 6))
print('\n-----Three Dimensional Random Array----')
print(arr2)
print()
print(np.greater_equal(arr2, 7))
Python Numpy Array greater_equal function

Python Numpy Array less

The Python Numpy less function checks whether the elements in a given array is less than a specified number or not. If True, boolean True returned otherwise, False. The syntax of this Python Numpy less function is

numpy.less(array_name, integer_value).

Within this example,

  • np.less(arr, 4) – check whether items in arr array is less than 4. 
  • np.less(arr1, 32) – check the items in 2D array arr1 is less than 32. 
  • np.less(arr2, 15) – check items in randomly generated 3D array are less than 15.
import numpy as np
 
arr = np.array([0, 2, 3, 0, 1, 6, 5, 2])
print('Original Array = ', arr)
print('Less Than 4 = ', np.less(arr, 4))
 
arr1 = np.random.randint(10, 50, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print()
print(np.less(arr1, 32))
 
arr2 = np.random.randint(1, 25, size = (2, 3, 6))
print('\n-----Three Dimensional Random Array----')
print(arr2)
print()
print(np.less(arr2, 15))

Python Numpy Array less function output

Original Array =  [0 2 3 0 1 6 5 2]
Less Than 4 =  [ True  True  True  True  True False False  True]

-----Two Dimensional Random Array----
[[42 49 34 12 38 12 34 33]
 [24 13 47 15 41 10 20 14]
 [24 46 30 41 11 47 37 36]
 [26 40 13 10 34 38 36 11]
 [19 46 37 17 38 15 38 19]]

[[False False False  True False  True False False]
 [ True  True False  True False  True  True  True]
 [ True False  True False  True False False False]
 [ True False  True  True False False False  True]
 [ True False False  True False  True False  True]]

-----Three Dimensional Random Array----
[[[ 4 24 22  4 12 10]
  [23 24  7 13  4  8]
  [12  9 23  3  2  1]]

 [[22 19 18 21  7 16]
  [11 17 12  2 21 14]
  [ 1 15 16  7  4  5]]]

[[[ True False False  True  True  True]
  [False False  True  True  True  True]
  [ True  True False  True  True  True]]

 [[False False False False  True False]
  [ True False  True  True False  True]
  [ True False False  True  True  True]]]

Python Numpy Array less_equal

The Python Numpy less_equal function checks whether each element in a given array is less than or equal to a specified number or not. If True, boolean True returned otherwise, False. The syntax of this Python Numpy less_equal function is.

numpy.less_equal(array_name, integer_value).

Within this example,

  • np.less_equal(arr, 3) – check whether items in arr array is less than or equal to 3. 
  • np.less_equal(arr1, 30) – check the items in 2D array arr1 is less than or equal to 30. 
  • np.less_equal(arr2, 9) – check whether items in the randomly generated three-dimensional array are less than or equal to 9.
import numpy as np
 
arr = np.array([0, 2, 3, 0, 1, 6, 5, 2])
print('Original Array = ', arr)
print('Less Than or Equal to 3 = ', np.less_equal(arr, 3))
 
arr1 = np.random.randint(10, 50, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print()
print(np.less_equal(arr1, 30))
 
arr2 = np.random.randint(1, 15, size = (2, 3, 6))
print('\n-----Three Dimensional Random Array----')
print(arr2)
print()
print(np.less_equal(arr2, 9))

Numpy Array less_equal output

Original Array =  [0 2 3 0 1 6 5 2]
Less Than or Equal to 3 =  [ True  True  True  True  True False False  True]

-----Two Dimensional Random Array----
[[13 21 24 37 34 19 43 20]
 [19 32 28 41 34 36 41 10]
 [23 29 37 49 24 39 48 12]
 [34 26 20 36 18 36 22 25]
 [25 43 33 15 13 45 36 13]]

[[ True  True  True False False  True False  True]
 [ True False  True False False False False  True]
 [ True  True False False  True False False  True]
 [False  True  True False  True False  True  True]
 [ True False False  True  True False False  True]]

-----Three Dimensional Random Array----
[[[ 3  6  7  2  9  1]
  [ 9  4  2  3  2  8]
  [ 2  4 11  8  2  6]]

 [[ 5  7 14  3 12 13]
  [ 5 13  6  3 10  8]
  [14  8  3  3 12  5]]]

[[[ True  True  True  True  True  True]
  [ True  True  True  True  True  True]
  [ True  True False  True  True  True]]

 [[ True  True False  True False False]
  [ True False  True  True False  True]
  [False  True  True  True False  True]]]

Python Array equal

The Python Numpy equal function checks whether each item in an array is equal to a given number or not. If True, boolean True returned otherwise, False. The syntax of this Python Numpy equal function is 

numpy.equal(array_name, integer_value).

Within this example,

  • np.equal(arr, 0) – check whether items in arr array is equal to 0. 
  • np.equal(arr1, 28) – check items in two dimensional array arr1 is equal to 28. 
  • np.equal(arr2, 8) – check 3D array items are equal to 8.
import numpy as np
 
arr = np.array([0, 2, 3, 0, 1, 6, 0, 2])
print('Original Array = ', arr)
print('Equal to 0 = ', np.equal(arr, 0))
 
arr1 = np.random.randint(20, 30, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print()
print(np.equal(arr1, 28))
 
arr2 = np.random.randint(1, 10, size = (2, 3, 6))
print('\n-----Three Dimensional Random Array----')
print(arr2)
print()
print(np.equal(arr2, 8))

Numpy Array equal function output

Original Array =  [0 2 3 0 1 6 0 2]
Equal to 0 =  [ True False False  True False False  True False]

-----Two Dimensional Random Array----
[[20 23 20 26 22 23 22 22]
 [22 27 25 29 24 25 24 22]
 [27 22 22 27 22 22 20 25]
 [26 28 28 27 28 28 24 22]
 [25 28 21 24 27 26 24 21]]

[[False False False False False False False False]
 [False False False False False False False False]
 [False False False False False False False False]
 [False  True  True False  True  True False False]
 [False  True False False False False False False]]

-----Three Dimensional Random Array----
[[[8 2 8 3 1 4]
  [2 9 2 6 9 8]
  [4 1 6 6 5 5]]

 [[1 2 7 7 8 4]
  [3 7 9 5 7 7]
  [1 2 8 8 9 7]]]

[[[ True False  True False False False]
  [False False False False False  True]
  [False False False False False False]]

 [[False False False False  True False]
  [False False False False False False]
  [False False  True  True False False]]]

Python Array not_equal

The Python Numpy not_equal function checks whether each item in an array is not equals to a given number or not. If True, boolean True returned otherwise, False. The syntax of this Python Numpy not_equal function is 

numpy.equal(array_name, integer_value).

Within this example,

  • np.not_equal(arr, 0) – check whether items in arr array is not equal to 0. 
  • np.not_equal(arr1, 25) – check items in two dimensional array arr1 is not equal to 25. 
  • np.not_equal(arr2, 6) – check 3D array items are not equal to 6.
import numpy as np
 
arr = np.array([0, 2, 3, 0, 1, 6, 0, 2])
print('Original Array = ', arr)
print('Equal to 0 = ', np.not_equal(arr, 0))
 
arr1 = np.random.randint(20, 30, size = (5, 8))
print('\n-----Two Dimensional Random Array----')
print(arr1)
print()
print(np.not_equal(arr1, 25))
 
arr2 = np.random.randint(1, 10, size = (2, 3, 6))
print('\n-----Three Dimensional Random Array----')
print(arr2)
print()
print(np.not_equal(arr2, 6))

Numpy Array not_equal function output

Original Array =  [0 2 3 0 1 6 0 2]
Equal to 0 =  [False  True  True False  True  True False  True]

-----Two Dimensional Random Array----
[[23 25 28 24 24 26 27 24]
 [25 29 23 22 22 20 21 20]
 [27 26 25 22 20 27 22 21]
 [24 24 21 28 27 24 22 21]
 [25 23 24 24 26 28 20 24]]

[[ True False  True  True  True  True  True  True]
 [False  True  True  True  True  True  True  True]
 [ True  True False  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True]
 [False  True  True  True  True  True  True  True]]

-----Three Dimensional Random Array----
[[[1 3 1 7 6 8]
  [6 3 1 7 7 8]
  [4 4 3 6 3 9]]

 [[8 8 1 8 9 4]
  [7 6 6 7 8 1]
  [3 1 1 4 2 2]]]

[[[ True  True  True  True False  True]
  [False  True  True  True  True  True]
  [ True  True  True False  True  True]]

 [[ True  True  True  True  True  True]
  [ True False False  True  True  True]
  [ True  True  True  True  True  True]]]

Python Numpy > Operator

The Python Numpy > Operator is the same as a greater function. Either you can use this to compare each element in an array with a static value, or use this to compare two arrays or matrixes.

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 5, 2])
 
print('Original Array = ', x)
print('x Greater Than 2 = ', x > 2)
Python Numpy > Operator Greater Than

This time, we are using both the > operator and greater function to compare two one dimensional arrays and check whether items in one array is greater than the other.

import numpy as np
 
a = np.array([2, 4])
b = np.array([2, 3])

print('a Greater Than b = ', a > b)
print('a Greater Than b = ', np.greater(a, b))
 
arr1 = np.random.randint(1, 8, size = (7))
arr2 = np.random.randint(1, 9, size = (7))
print('\n-----Random Array----')
print('Values in arr1 = ', arr1)
print('Values in arr2 = ', arr2)
print()

print('Result of arr1 > arr2 = ', arr1 > arr2)
print('greater(arr1, arr2)   = ', np.greater(arr1, arr2))
Python Numpy > Operator 2

In the above example, we used only a one-dimensional array. However, this time, we compare two dimensional and multi-dimensional arrays to find the maximum values in two arrays.

import numpy as np
 
arr1 = np.random.randint(1, 8, size = (3, 5))
arr2 = np.random.randint(1, 9, size = (3, 5))

print('-----Two Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
 
print('Result of arr1 > arr2 = \n', arr1 > arr2)
print('greater(arr1, arr2)   = \n', np.greater(arr1, arr2))
 
arr1 = np.random.randint(1, 8, size = (1, 2, 8))
arr2 = np.random.randint(1, 9, size = (1, 2, 8))

print('\n-----Three Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
 
print('Result of arr1 > arr2 = \n', arr1 > arr2)
print('greater(arr1, arr2)   = \n', np.greater(arr1, arr2))

Numpy Array > operator and greater function output

-----Two Dimensional Random Array----
Values in arr1 = 
 [[2 6 2 5 2]
 [2 3 7 4 7]
 [7 4 1 5 4]]
Values in arr2 = 
 [[6 3 5 8 7]
 [8 3 4 1 2]
 [2 7 7 6 2]]
Result of arr1 > arr2 = 
 [[False  True False False False]
 [False False  True  True  True]
 [ True False False False  True]]
greater(arr1, arr2)   = 
 [[False  True False False False]
 [False False  True  True  True]
 [ True False False False  True]]

-----Three Dimensional Random Array----
Values in arr1 = 
 [[[7 2 7 2 4 6 5 5]
  [6 6 4 1 4 2 1 7]]]
Values in arr2 = 
 [[[3 5 1 8 8 6 5 7]
  [1 6 5 6 2 1 2 5]]]
Result of arr1 > arr2 = 
 [[[ True False  True False False False False False]
  [ True False False False  True  True False  True]]]
greater(arr1, arr2)   = 
 [[[ True False  True False False False False False]
  [ True False False False  True  True False  True]]]

Python Numpy >= Operator

The Python Numpy >= Operator is the same as the greater_equal function. You can use >= operator to compare array elements with a static value or find greater than equal values in two arrays or matrixes.

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 5, 2])
 
print('Original Array = ', x)
print('x Greater Than or Equal to 3 = \n', x >= 3)
Python Numpy >= Operator 1

Here, we are using both the >= operator and greater_equal function to compare two one dimensional arrays. Below code returns true if the value in a or arr1 is greater than or equal to b and arr2 otherwise, False.

import numpy as np
 
a = np.array([2, 4])
b = np.array([2, 7])
print('a Greater Than or Equal to b = ', a >= b)
print('a Greater Than or Equal to b = ', np.greater_equal(a, b))
 
arr1 = np.random.randint(1, 8, size = (7))
arr2 = np.random.randint(1, 9, size = (7))

print('\n-----Random Array----')
print('Values in arr1 = ', arr1)
print('Values in arr2 = ', arr2)
print()
print('Result of arr1 >= arr2      = ', arr1 >= arr2)
print('greater_equal(arr1, arr2)   = ', np.greater_equal(arr1, arr2))
Python Numpy >= Operator 2

Let me use them on two and three-dimensional arrays

import numpy as np
 
arr1 = np.random.randint(1, 8, size = (3, 5))
arr2 = np.random.randint(1, 9, size = (3, 5))
 
print('-----Two Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
 
print('Result of arr1 >= arr2 = \n', arr1 >= arr2)
print('greater_equal(arr1, arr2)   = \n', np.greater_equal(arr1, arr2))
 
arr1 = np.random.randint(1, 8, size = (1, 2, 8))
arr2 = np.random.randint(1, 9, size = (1, 2, 8))
 
print('\n-----Three Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
 
print('Result of arr1 >= arr2      = \n', arr1 >= arr2)
print('greater_equal(arr1, arr2)   = \n', np.greater_equal(arr1, arr2))

Numpy Array >= operator and greater_equal function output

-----Two Dimensional Random Array----
Values in arr1 = 
 [[5 7 3 4 4]
 [5 2 3 2 5]
 [5 1 6 7 7]]
Values in arr2 = 
 [[7 7 1 1 4]
 [1 5 2 7 4]
 [8 4 7 1 6]]
Result of arr1 >= arr2 = 
 [[False  True  True  True  True]
 [ True False  True False  True]
 [False False False  True  True]]
greater_equal(arr1, arr2)   = 
 [[False  True  True  True  True]
 [ True False  True False  True]
 [False False False  True  True]]

-----Three Dimensional Random Array----
Values in arr1 = 
 [[[5 7 3 1 6 2 5 7]
  [5 4 3 5 4 2 2 4]]]
Values in arr2 = 
 [[[6 3 3 2 3 2 1 1]
  [7 7 6 5 8 7 8 2]]]
Result of arr1 >= arr2      = 
 [[[False  True  True False  True  True  True  True]
  [False False False  True False False False  True]]]
greater_equal(arr1, arr2)   = 
 [[[False  True  True False  True  True  True  True]
  [False False False  True False False False  True]]]

Python Numpy < Operator

The Python Numpy < Operator is the same as less function. Either you can use this to check whether each element in an array is less than a static value or another array or matrix.

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 5, 2])
 
print('Original Array = ', x)
print('x Less Than 2 = ', x < 2)
Python Numpy < Operator 1

This time, we use both the < operator and greater function to check whether items in a two dimensional and multi-dimensional array is less than the items in another array or not. If True, Boolean True returned otherwise, false.

import numpy as np
 
arr1 = np.random.randint(1, 8, size = (3, 5))
arr2 = np.random.randint(1, 9, size = (3, 5))
 
print('-----Two Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
 
print('Result of arr1 > arr2 = \n', arr1 < arr2)
print('less(arr1, arr2)   = \n', np.less(arr1, arr2))
 
arr1 = np.random.randint(1, 8, size = (1, 2, 8))
arr2 = np.random.randint(1, 9, size = (1, 2, 8))
 
print('\n-----Three Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
 
print('Result of arr1 > arr2 = \n', arr1 < arr2)
print('less(arr1, arr2)   = \n', np.less(arr1, arr2))

Numpy Array < operator and greater function output

-----Two Dimensional Random Array----
Values in arr1 = 
 [[6 7 3 3 6]
 [2 3 5 3 3]
 [6 7 2 2 2]]
Values in arr2 = 
 [[8 5 3 2 2]
 [7 8 3 3 3]
 [7 1 8 6 2]]
Result of arr1 > arr2 = 
 [[ True False False False False]
 [ True  True False False False]
 [ True False  True  True False]]
less(arr1, arr2)   = 
 [[ True False False False False]
 [ True  True False False False]
 [ True False  True  True False]]

-----Three Dimensional Random Array----
Values in arr1 = 
 [[[2 5 6 3 7 5 4 1]
  [3 7 5 1 3 3 6 7]]]
Values in arr2 = 
 [[[1 4 2 6 4 6 8 6]
  [1 6 6 1 3 4 1 4]]]
Result of arr1 > arr2 = 
 [[[False False False  True False  True  True  True]
  [False False  True False False  True False False]]]
less(arr1, arr2)   = 
 [[[False False False  True False  True  True  True]
  [False False  True False False  True False False]]]

Python Numpy <= Operator

The Python Numpy <= Operator is the same as the less_equal function. Use Numpy <= operator to check array items are less than or equal to a number or another array.

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 5, 2])
 
print('Original Array = ', x)
print('x Less Than or Equal to 3 = \n', x <= 3)
Python Numpy <= Operator 1

Here, we use both the Python Numpy <= operator and Python less_equal function to compare two and three-dimensional arrays. The below code returns true if a value in arr1 is less than or equal to arr2 otherwise, False.

import numpy as np
 
arr1 = np.random.randint(1, 8, size = (3, 5))
arr2 = np.random.randint(1, 9, size = (3, 5))
 
print('-----Two Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
print('Result of arr1 <= arr2 = \n', arr1 <= arr2)
print('less_equal(arr1, arr2)   = \n', np.less_equal(arr1, arr2))
 
arr1 = np.random.randint(1, 8, size = (1, 2, 8))
arr2 = np.random.randint(1, 9, size = (1, 2, 8))
print('\n-----Three Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
print('Result of arr1 <= arr2      = \n', arr1 <= arr2)
print('less_equal(arr1, arr2)   = \n', np.less_equal(arr1, arr2))

Numpy array <= operator and less_equal function output

-----Two Dimensional Random Array----
Values in arr1 = 
 [[7 2 6 3 7]
 [2 7 1 7 3]
 [3 3 3 1 2]]
Values in arr2 = 
 [[1 6 2 6 1]
 [8 4 8 7 3]
 [8 2 8 2 7]]
Result of arr1 <= arr2 = 
 [[False  True False  True False]
 [ True False  True  True  True]
 [ True False  True  True  True]]
less_equal(arr1, arr2)   = 
 [[False  True False  True False]
 [ True False  True  True  True]
 [ True False  True  True  True]]

-----Three Dimensional Random Array----
Values in arr1 = 
 [[[3 1 2 2 6 3 7 4]
  [6 6 5 4 6 7 7 6]]]
Values in arr2 = 
 [[[7 4 5 2 5 5 1 7]
  [3 8 7 6 2 1 7 5]]]
Result of arr1 <= arr2      = 
 [[[ True  True  True  True False  True False  True]
  [False  True  True  True False False  True False]]]
less_equal(arr1, arr2)   = 
 [[[ True  True  True  True False  True False  True]
  [False  True  True  True False False  True False]]]

Python Numpy == Operator

The Python Numpy == Operator is the same as equal function. Use == operator to check whether array items are equal to a number or another array. 

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 0, 2])
 
print('Original Array = ', x)
print('x Equal to 0 = ', x == 0)
Python Numpy == Operator 1

Here, we are using both the == operator and equal function to compare 2D and 3D arrays. The below code returns true if the value in arr1 is equal to arr2 otherwise, False.

import numpy as np
 
arr1 = np.random.randint(1, 8, size = (3, 5))
arr2 = np.random.randint(1, 9, size = (3, 5))
print('-----Two Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
print('Result of arr1 == arr2 = \n', arr1 == arr2)
print('equal(arr1, arr2)   = \n', np.equal(arr1, arr2))
 
arr1 = np.random.randint(1, 8, size = (1, 2, 8))
arr2 = np.random.randint(1, 9, size = (1, 2, 8))
print('\n-----Three Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
print('Result of arr1 == arr2      = \n', arr1 == arr2)
print('equal(arr1, arr2)   = \n', np.equal(arr1, arr2))

Numpy Array == operator and equal function output

-----Two Dimensional Random Array----
Values in arr1 = 
 [[2 2 6 4 2]
 [5 5 6 5 2]
 [1 4 5 2 3]]
Values in arr2 = 
 [[1 8 1 7 6]
 [1 5 4 2 5]
 [6 1 7 1 6]]
Result of arr1 == arr2 = 
 [[False False False False False]
 [False  True False False False]
 [False False False False False]]
equal(arr1, arr2)   = 
 [[False False False False False]
 [False  True False False False]
 [False False False False False]]

-----Three Dimensional Random Array----
Values in arr1 = 
 [[[5 2 4 7 2 6 1 4]
  [7 3 1 7 5 7 2 7]]]
Values in arr2 = 
 [[[1 3 7 6 6 4 1 4]
  [6 4 1 4 1 5 3 6]]]
Result of arr1 == arr2      = 
 [[[False False False False False False  True  True]
  [False False  True False False False False False]]]
equal(arr1, arr2)   = 
 [[[False False False False False False  True  True]
  [False False  True False False False False False]]]

Python Numpy != Operator

The Python Numpy != Operator is the same as the not_equal function. Use != operator to check whether items in one array are not equal to a number or another array. 

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 0, 2])
 
print('Original Array = ', x)
print('x Not Equal to 0 = \n', x != 0)

Numpy Array != Operator output

Original Array =  [0 2 3 0 1 6 0 2]
x Not Equal to 0 = 
 [False  True  True False  True  True False  True]

Here, we use both the Python Numpy != operator and Python not_equal function to compare 2D and three-dimensional arrays. It returns true, if array values in arr1 are not equal to arr2 otherwise, False.

import numpy as np
 
arr1 = np.random.randint(1, 8, size = (3, 5))
arr2 = np.random.randint(1, 9, size = (3, 5))
print('-----Two Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
print('Result of arr1 != arr2 = \n', arr1 != arr2)
print('not_equal(arr1, arr2)   = \n', np.not_equal(arr1, arr2))
 
arr1 = np.random.randint(1, 8, size = (1, 2, 8))
arr2 = np.random.randint(1, 9, size = (1, 2, 8))
print('\n-----Three Dimensional Random Array----')
print('Values in arr1 = \n', arr1)
print('Values in arr2 = \n', arr2)
print('Result of arr1 != arr2      = \n', arr1 != arr2)
print('not_equal(arr1, arr2)   = \n', np.not_equal(arr1, arr2))

Numpy Array != operator and not_equal function output

-----Two Dimensional Random Array----
Values in arr1 = 
 [[6 2 3 3 3]
 [4 5 2 7 7]
 [2 1 7 7 4]]
Values in arr2 = 
 [[6 1 5 6 3]
 [2 3 3 8 6]
 [7 7 5 2 4]]
Result of arr1 != arr2 = 
 [[False  True  True  True False]
 [ True  True  True  True  True]
 [ True  True  True  True False]]
not_equal(arr1, arr2)   = 
 [[False  True  True  True False]
 [ True  True  True  True  True]
 [ True  True  True  True False]]

-----Three Dimensional Random Array----
Values in arr1 = 
 [[[1 2 2 1 2 4 6 1]
  [2 6 5 4 7 2 2 3]]]
Values in arr2 = 
 [[[8 3 7 3 5 2 6 1]
  [5 4 5 4 3 3 2 2]]]
Result of arr1 != arr2      = 
 [[[ True  True  True  True  True  True False False]
  [ True  True False False  True  True False  True]]]
not_equal(arr1, arr2)   = 
 [[[ True  True  True  True  True  True False False]
  [ True  True False False  True  True False  True]]]

Python Numpy alltrue and sometrue functions 

The Python Numpy alltrue function is similar to the If statement. If the condition inside this function is true, then True returned otherwise, False. For example, np.alltrue(np.greater(x, 2)) – It returns True if all the elements in x array are greater than 2, then True returned; otherwise, this function return False.

The Python Numpy sometrue function returns true if at least one element in the specified array has to meet the condition otherwise, False. For example, np.alltrue(np.less(x, 3)) – It returns True if at least one element or one array item is less than 3 otherwise, this function return False.

import numpy as np
x = np.array([0, 2, 3, 0, 1, 6, 5, 2])
print('Original Array x = \n', x)
 
print('\nalltrue of x = ', np.alltrue(np.greater(x, 2)))
print('sometrue of x = ', np.sometrue(np.greater(x, 2)))
 
print('\nalltrue of x = ', np.alltrue(np.greater_equal(x, 0)))
print('sometrue of x = ', np.sometrue(np.greater_equal(x, 10)))
 
print('\nalltrue of x = ', np.alltrue(np.less(x, 3)))
print('sometrue of x = ', np.sometrue(np.less(x, 3)))
print('alltrue of x = ', np.alltrue(np.less(x, 7)))
 
print('\nalltrue of x = ', np.alltrue(np.less_equal(x, 6)))
print('sometrue of x = ', np.sometrue(np.less_equal(x, -1)))

Numpy Array alltrue and sometrue functions output

Original Array x = 
 [0 2 3 0 1 6 5 2]

alltrue of x =  False
sometrue of x =  True

alltrue of x =  True
sometrue of x =  False

alltrue of x =  False
sometrue of x =  True
alltrue of x =  True

alltrue of x =  True
sometrue of x =  False