{"id":5820,"date":"2021-05-16T10:43:44","date_gmt":"2021-05-16T05:13:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=5820"},"modified":"2021-11-22T18:49:05","modified_gmt":"2021-11-22T13:19:05","slug":"python-check-if-all-values-are-same-in-a-numpy-array-both-1d-and-2d","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-check-if-all-values-are-same-in-a-numpy-array-both-1d-and-2d\/","title":{"rendered":"Python: Check if all values are same in a Numpy Array (both 1D and 2D)"},"content":{"rendered":"
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.<\/p>\n
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 If all elements in bool array become true then all elements of main array are same.<\/p>\n 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.<\/p>\n 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.<\/strong><\/p>\n Similarly to check if all elements are same in each row, we can compare elements of each row with first element.<\/p>\n Here also to check if all elements are same in each column, we can compare elements of each column with first element.<\/p>\n \u00a0<\/strong><\/p>\n 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 …<\/p>\ntrue<\/code> is returned else
false<\/code> is returned.<\/p>\n
import numpy as sc\r\n\r\n# creating a 1D numpy array\r\n\r\nnum_arr = sc.array([14, 14, 14, 14, 14, 14])\r\n\r\n# here we check if all values in array is same as first element\r\n\r\nres = sc.all(num_arr == num_arr[0])\r\n\r\nif res:\r\n\r\n print('All elements are same')\r\n\r\nelse:\r\n\r\n print('All elements are not same')<\/pre>\n
Output :\r\nAll elements are same<\/pre>\n
Check if all elements are equal in a 1D Numpy Array using min() & max() :<\/h3>\n
import numpy as sc\r\n\r\n# creating a 1D numpy array\r\n\r\nnum_arr = sc.array([14, 14, 14, 14, 14, 14])\r\n\r\n# if min element= max element then all values in array are same\r\n\r\nres = sc.max(num_arr) == sc.min(num_arr)\r\n\r\nif res:\r\n\r\n print('All elements are equal')\r\n\r\nelse:\r\n\r\n print('All elements are not equal')<\/pre>\n
Output :\r\nAll elements are equal<\/pre>\n
Check if all elements are equal in a Multidimensional Numpy Array or Matrix :<\/h3>\n
import numpy as sc\r\n\r\nmul_arr = sc.array([[1, 1, 1],\r\n\r\n[1, 1, 1],\r\n\r\n[1, 1, 1]])\r\n\r\n# to get a flattened 1D view of multidimensional numpy array\r\n\r\nflatn_arr = sc.ravel(mul_arr)\r\n\r\n# to check if all value in multidimensional array are equal\r\n\r\nres = sc.all(mul_arr==flatn_arr[0])\r\n\r\nif res:\r\n\r\n print('All elements are same')\r\n\r\nelse:\r\n\r\n print('All elements are not same')<\/pre>\n
Output :\r\nAll elements are same<\/pre>\n
Find rows or columns with same values in a matrix or 2D Numpy array :<\/h3>\n
Find rows with same values in a matrix or 2D Numpy array :<\/h4>\n
import numpy as sc\r\nmul_arr = sc.array([[1, 1, 1],\r\n[1, 21, 1],\r\n[1, 1, 1]])\r\n# to compare 1st element with elements of each row\r\nfor i in range(mul_arr.shape[0]):\r\n if sc.all(mul_arr[i]==mul_arr[i][0]):\r\n print('Row:', i)\r\nprint(\"Following rows have same elements\")<\/pre>\n
Output :\r\nRow: 0\r\nRow: 2\r\nFollowing rows have same elements<\/pre>\n
Find columns with same values in a matrix or 2D Numpy array :<\/h4>\n
import numpy as sc\r\nmul_arr = sc.array([[1, 1, 1],\r\n[1, 21, 1],\r\n[1, 1, 1]])\r\n#to compare 1st element with elements of each row\r\ntrans_Arra = mul_arr.T\r\nfor i in range(trans_Arra.shape[0]):\r\n if sc.all(trans_Arra[i] == trans_Arra[i][0]):\r\n print('Column: ', i)\r\nprint(\"Following rows have same elements\")<\/pre>\n
Output :\r\nColumn:\u00a0 0\r\nColumn:\u00a0 2\r\nFollowing rows have same elements<\/pre>\n","protected":false},"excerpt":{"rendered":"