{"id":6755,"date":"2023-10-31T10:28:51","date_gmt":"2023-10-31T04:58:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=6755"},"modified":"2023-11-10T12:10:30","modified_gmt":"2023-11-10T06:40:30","slug":"numpy-count_nonzero-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/numpy-count_nonzero-python\/","title":{"rendered":"numpy.count_nonzero() \u2013 Python"},"content":{"rendered":"
In this article we will discuss about how to count values based on conditions in 1D or 2D Numpy Arrays using A function Where,<\/p>\n Which returns int or array of int containing count of non zero values in numpy array and if the Axis is provided then it returns the array of count of values along the axis.<\/p>\n Suppose we have a numpy array with some zeros and non zero values. Now we will count the non zero values using So let’s see the program to understand how it actually works.<\/p>\n As we know in python True is equivalent to 1 and False is equivalent to 0 then we can use the It is very simple to count the non-zero values as we did in previous example we only passed the complete numpy array , here we will pass the condition.<\/p>\n So lets see the example to understand it clearly.<\/p>\n In the above example which element will satisfy the condition the value will be True and which will not satisfy the value will be false. And it will count the True values.<\/p>\n By using the same So lets see the example to understand it clearly.<\/p>\n To count the non-zero values in each row of 2D numpy array just pass value of axis as 1.<\/p>\n So lets see the example to understand it clearly.<\/p>\n To count the non-zero values in each columnof 2D numpy array just pass value of axis as 0.<\/p>\n So lets see the example to understand it clearly.<\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Using numpy.count_nonzero() Function In this article we will discuss about how to count values based on conditions in 1D or 2D Numpy Arrays using numpy.count_nonzero() function in python. So let’s explore the topic. numpy.count_nonzero() : A function numpy.count_nonzero() is provided by Numpy module in python to count the non-zero values in array, Syntax- numpy.count_nonzero(arr, axis=None, …<\/p>\nnumpy.count_nonzero()<\/code> function in python. So let’s explore the topic.<\/p>\n
numpy.count_nonzero() :<\/h3>\n
numpy.count_nonzero()<\/code> is provided by Numpy module in python to count the non-zero values in array,<\/p>\n
Syntax- numpy.<\/span>count_nonzero<\/span>(<\/span>arr, axis=<\/span>None<\/span>, keepdims=<\/span>False<\/span>)<\/span><\/pre>\n
\n
Counting non zero values in a Numpy Array :<\/h3>\n
numpy.count_nonzero()<\/code> function.<\/p>\n
# Program :\r\n\r\nimport numpy as np\r\n# numpy array from list created\r\narr = np.array([2, 3, 0, 5, 0, 0, 5, 0, 5])\r\n# Counting non zero elements in numpy array\r\ncount = np.count_nonzero(arr)\r\nprint('Total count of non-zero values in NumPy Array: ', count)<\/pre>\n
Output :\r\nTotal count of non-zero values in NumPy Array: 5<\/pre>\n
Counting True values in a numpy Array :<\/h3>\n
numpy.count_nonzero()<\/code> function to count the True values in a bool numpy array.<\/p>\n
# Program :\r\n\r\nimport numpy as np\r\n# Numpy Array of bool values created\r\narr = np.array([False, True, True, True, False, False, False, True, True])\r\n# Counting True elements in numpy array\r\ncount = np.count_nonzero(arr)\r\nprint('Total count of True values in NumPy Array: ', count)<\/pre>\n
Output :\r\nTotal count of True values in NumPy Array: 5<\/pre>\n
Counting Values in Numpy Array that satisfy a condition :<\/h3>\n
# Program :\r\n\r\nimport numpy as np\r\n# A Numpy array of numbers is created\r\narr = np.array([2, 3, 1, 5, 4, 2, 5, 6, 5])\r\n# Count even number of even elements in array\r\ncount = np.count_nonzero(arr % 2 == 0)\r\nprint('Total count of Even Numbers in Numpy Array: ', count)<\/pre>\n
Output :\r\nTotal count of Even Numbers in Numpy Array: 4<\/pre>\n
Counting Non-Zero Values in 2D Numpy Array :<\/h3>\n
numpy.count_nonzero()<\/code> function we can count the non-zero values in a 2D array where the default axis value is None.<\/p>\n
# Program :\r\n\r\nimport numpy as np\r\n# 2D Numpy Array created \r\narr_2d = np.array( [[20, 30, 0],\r\n [50, 0, 0],\r\n [50, 0, 50]])\r\n# counting of non zero values in complete 2D array\r\ncount = np.count_nonzero(arr_2d)\r\nprint('Total count of non zero values in complete 2D array: ', count)<\/pre>\n
Output :\r\nTotal count of non zero values in complete 2D array: 5<\/pre>\n
Counting Non-Zero Values in each row of 2D Numpy Array :<\/h3>\n
# Program :\r\n\r\nimport numpy as np\r\n# Create 2D Numpy ARray\r\narr = np.array( [[20, 30, 0],\r\n [50, 0, 0],\r\n [50, 0, 50]])\r\n# Get count of non zero values in each row of 2D array\r\ncount = np.count_nonzero(arr, axis=1)\r\nprint('Total count of non zero values in each row of 2D array: ', count)<\/pre>\n
Output :\r\nTotal count of non zero values in each row of 2D array: [<\/span>2<\/span> 1<\/span> 2<\/span>]<\/span><\/pre>\n
Counting Non-Zero Values in each column of 2D Numpy Array :<\/h3>\n
# Program :\r\n\r\nimport numpy as np\r\n# 2D Numpy Array created\r\narr = np.array( [[20, 30, 0],\r\n [50, 0, 0],\r\n [50, 0, 50]])\r\n# counting of non zero values in each column of 2D array\r\ncount = np.count_nonzero(arr, axis=0)\r\nprint('Total count of non zero values in each column of 2D array: ', count)<\/pre>\n
Output :\r\nTotal count of non zero values in each column of 2D array: [<\/span>3<\/span> 1<\/span> 1<\/span>]<\/span><\/pre>\n