{"id":5823,"date":"2023-10-30T10:46:04","date_gmt":"2023-10-30T05:16:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=5823"},"modified":"2023-11-10T12:08:21","modified_gmt":"2023-11-10T06:38:21","slug":"python-count-number-of-true-elements-in-numpy-array-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-count-number-of-true-elements-in-numpy-array-in-python\/","title":{"rendered":"Python: Count Number of True Elements in Numpy Array in Python"},"content":{"rendered":"
In this article, we will study what are true elements in python and different methods to calculate the count of true elements in both 1-D and 2-D Numpy array. First, let see what are true elements in python.<\/p>\n
True elements in numpy array are equivalent to 1 and False element is equivalent to 0. So we can say that it is basically a boolean numpy array.<\/p>\n
Now let us see different methods to count a number of True elements in a 1-D numpy array.<\/p>\n
This is one of the methods to count a number of true elements in a numpy array. count_nonzero() method require 1 argument i.e. the name of numpy array and then it return the count of true element in numpy array. It also accepts axis as one of the arguments but we do not need it in this case.<\/p>\n
Syntax: count_nonzero(arr, axis=None)<\/p>\n
Let see this with the help of an example.<\/p>\n
import numpy as np\r\n\r\narr1 = np.array([False, True, True, False,True])\r\narr2 = np.array([0,1,1,0,1])\r\ncount1 = np.count_nonzero(arr1)\r\ncount2 = np.count_nonzero(arr2)\r\nprint('Print count of True elements in array1: ', count1)\r\nprint('Print count of True elements in array2: ', count2)<\/pre>\nOutput<\/p>\n
Print count of True elements in array1: 3\r\nPrint count of True elements in array2: 3<\/span><\/pre>\n
There are two things to notice in this example first is that in arr1 count of True is 3 hence we get output as 3 and the second thing to notice is that if we write 1 and 0 instead of True and False respectively then we will get the same output. Hence it is also verified that True is equivalent to 1 and False is equivalent to 0 in a numpy array.<\/p>\n
\n
- \n
Method 2-Using sum() function<\/h3>\n<\/li>\n<\/ul>\n
sum() is another method to count a number of true elements in a numpy array. Here sum() method works in the same way that we see in basic python. It returns the sum of all the elements in a Numpy array. As True is equivalent to 1 and False is equivalent to 0 so the sum we get is equal to count of True elements.<\/p>\n
syntax: sum(arr)<\/p>\n
Let see this with the help of an example.<\/p>\n
import numpy as np\r\n\r\narr1 = np.array([False, True, True, False,True])\r\ncount1 = sum(arr1)\r\nprint('Print count of True elements in array1: ', count1)<\/pre>\nOutput<\/p>\n
Print count of True elements in array1: 3<\/span><\/pre>\n
\n
- \n
Method 3 Using bincount() method<\/h3>\n<\/li>\n<\/ul>\n
bincount() return list of occurrence of each value in numpy array. Let see this with an example.<\/p>\n
import numpy as np\r\n\r\narr1 = np.array([False, True, True, False,True])\r\nl = np.bincount(arr1)\r\nprint(l)<\/pre>\nOutput<\/p>\n
[2 3]<\/span><\/pre>\n
Here we see that at index 0 it return the count of the number of false values and at index 1 it return a count of true values. So now using list indexing we can return the count of elements at the 1st index which is the count of True values in the Numpy array.<\/p>\n
So these are the methods to count a number of True values in a 1-D Numpy array.<\/p>\n
Now we see that how to calculate the count of numpy array in the 2-D array. The best part is that the above methods that we study are used to calculate the count of True elements in a 2-D numpy array but the difference is that we can give an extra parameter as the axis to calculate the count of True elements row-wise or column-wise.<\/p>\n
\n
- \n
Method 1-Using count_nonzero() method<\/h3>\n<\/li>\n<\/ul>\n
The working of the method is the same that we study in the case of a 1-D numpy array. But here we have a choice that we can give the axis as a parameter or not. If we do not give axis as a parameter then this method will return the total count of True elements in a numpy array but if we give an axis it returns a list of the count of True element row-wise or column-wise. There are two axis<\/p>\n
axis=1 means return count row-wise<\/p>\n
axis=0 means return count column-wise<\/p>\n
Let see this with the help of an example.<\/p>\n
import numpy as np\r\n\r\narr_2d = np.array([ [False, True, True,False],\r\n [True, False, True,False],\r\n [False, True, True,False]])\r\ncount1 = np.count_nonzero(arr_2d)\r\ncount2 = np.count_nonzero(arr_2d,axis=1)\r\ncount3 = np.count_nonzero(arr_2d,axis=0)\r\nprint(\"Total count of True values\",count1)\r\nprint(\"Count of True values row-wise\",count2)\r\nprint(\"Count of True values column-wise\",count3)<\/pre>\nOutput<\/p>\n
<\/p>\n
Total count of True values 6\r\n<\/span>Count of True values row-wise [2 2 2]\r\nCount of True values column-wise [1 2 3 0]<\/span><\/pre>\n
\n
- \n
Method 2-Using sum() method<\/h3>\n<\/li>\n<\/ul>\n
The working of the method is the same that we study in the case of a 1-D numpy array. But here we have a choice that we can give the axis as a parameter or not. If we do not give axis as a parameter then this method will return the total count of True elements in a numpy array but if we give an axis it returns a list of the count of True element row-wise or column-wise. There are two axis<\/p>\n
axis=1 means return count row-wise<\/p>\n
axis=0 means return count column-wise<\/p>\n
Here we have to take care of one this that we can’t simply write sum() here we have to write np.sum() because we use axis as one of argument.<\/p>\n
Let see this with the help of an example.<\/p>\n
import numpy as np\r\n\r\narr_2d = np.array([ [False, True, True,False],\r\n [True, False, True,False],\r\n [False, True, True,False]])\r\ncount1 = np.sum(arr_2d)\r\ncount2 = np.sum(arr_2d,axis=1)\r\ncount3 = np.sum(arr_2d,axis=0)\r\nprint(\"Total count of True values\",count1)\r\nprint(\"Count of True values row-wise\",count2)\r\nprint(\"Count of True values column-wise\",count3)<\/pre>\nOutput<\/p>\n
Total count of True values 6\r\n<\/span>Count of True values row-wise [2 2 2]\r\nCount of True values column-wise [1 2 3 0]<\/span><\/pre>\n
So these are the methods to count the number of True values in both 1-D and 2-D numpy array.<\/p>\n","protected":false},"excerpt":{"rendered":"
Method to find count number of true element in numpy array in python In this article, we will study what are true elements in python and different methods to calculate the count of true elements in both 1-D and 2-D Numpy array. First, let see what are true elements in python. True Elements True elements …<\/p>\n