Python: Check if all values are same in a Numpy Array (both 1D and 2D)

Checking if all the values are same in Numpy Array (both 1D and 2D) :

In this article we will discuss how we can check if all elements are same in 1D or 2D. We will also check if all rows or columns have same values or not.

Check if all elements are equal in a 1D Numpy Array using numpy.all() :

Here we compare all the elements with the first element of the array and returns a bool array of same size. If first element is equal to a element in main array, then true is returned else false is returned.

If all elements in bool array become true then all elements of main array are same.

import numpy as sc

# creating a 1D numpy array

num_arr = sc.array([14, 14, 14, 14, 14, 14])

# here we check if all values in array is same as first element

res = sc.all(num_arr == num_arr[0])

if res:

 print('All elements are same')

else:

 print('All elements are not same')
Output :
All elements are same

Check if all elements are equal in a 1D Numpy Array using min() & max() :

If maximum value of element is equal to minimum value of element in array then it indirectly means all values are same in the array.

import numpy as sc

# creating a 1D numpy array

num_arr = sc.array([14, 14, 14, 14, 14, 14])

# if min element= max element then all values in array are same

res = sc.max(num_arr) == sc.min(num_arr)

if res:

 print('All elements are equal')

else:

 print('All elements are not equal')
Output :
All elements are equal

Check if all elements are equal in a Multidimensional Numpy Array or Matrix :

We know that numpy.ravel() function returns flattened 1D view of a array. So we can easily convert a multi-dimensional flattened to 1D array and then compare first element with all the elements to check all element are same or not.

import numpy as sc

mul_arr = sc.array([[1, 1, 1],

[1, 1, 1],

[1, 1, 1]])

# to get a flattened 1D view of multidimensional numpy array

flatn_arr = sc.ravel(mul_arr)

# to check if all value in multidimensional array are equal

res = sc.all(mul_arr==flatn_arr[0])

if res:

 print('All elements are same')

else:

 print('All elements are not same')
Output :
All elements are same

Find rows or columns with same values in a matrix or 2D Numpy array :

Find rows with same values in a matrix or 2D Numpy array :

Similarly to check if all elements are same in each row, we can compare elements of each row with first element.

import numpy as sc
mul_arr = sc.array([[1, 1, 1],
[1, 21, 1],
[1, 1, 1]])
# to compare 1st element with elements of each row
for i in range(mul_arr.shape[0]):
 if sc.all(mul_arr[i]==mul_arr[i][0]):
  print('Row:', i)
print("Following rows have same elements")
Output :
Row: 0
Row: 2
Following rows have same elements

Find columns with same values in a matrix or 2D Numpy array :

Here also to check if all elements are same in each column, we can compare elements of each column with first element.

 

import numpy as sc
mul_arr = sc.array([[1, 1, 1],
[1, 21, 1],
[1, 1, 1]])
#to compare 1st element with elements of each row
trans_Arra = mul_arr.T
for i in range(trans_Arra.shape[0]):
 if sc.all(trans_Arra[i] == trans_Arra[i][0]):
  print('Column: ', i)
print("Following rows have same elements")
Output :
Column:  0
Column:  2
Following rows have same elements

Leave a Comment