{"id":4835,"date":"2023-10-28T15:16:25","date_gmt":"2023-10-28T09:46:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=4835"},"modified":"2023-11-10T12:04:19","modified_gmt":"2023-11-10T06:34:19","slug":"count-occurrences-of-a-value-in-numpy-array-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/count-occurrences-of-a-value-in-numpy-array-in-python\/","title":{"rendered":"Count occurrences of a value in NumPy array in Python | numpy.count() in Python"},"content":{"rendered":"
Count Occurences of a Value in Numpy Array in Python:<\/strong> In this article, we have seen different methods to count the number of occurrences of a value in a NumPy array in Python. Check out the below given direct links and gain the information about Count occurrences of a value in a NumPy array in Python.<\/p>\n The function of An integer array with the number of non-overlapping occurrences of the substring.<\/p>\n <\/p>\n Output:<\/strong><\/p>\n Also Check:<\/span><\/p>\n Counting the occurrences of a value in a NumPy array means returns the frequency of the value in the array. Here are the various methods used to count the occurrences of a value in a python numpy array.<\/p>\n We use the Let’s take an example, count all occurrences of value \u20186\u2019 in an array,<\/p>\n Output:<\/strong><\/p>\n In the above example, you have seen that we applied a condition to the numpy array ie., arr==6, then it applies the condition on each element of the array and stores the result as a bool value in a new array.<\/p>\n In this, we are using the Output:<\/strong><\/p>\n In the above example, we have seen if the given condition is true then it is equivalent to one in python so we can add the True values in the array to get the sum of values in the array.<\/p>\n We use Output:<\/strong><\/p>\n In this method, first we convert the array to a list and then we apply Output:<\/strong><\/p>\n We can choose only those elements from the numpy array that is similar to a presented value and then we can attain the length of this new array. It will furnish the count of occurrences of the value in the original array. For instance,<\/p>\n Output:\u00a0<\/strong><\/p>\n We can use the count_nonzero() function to count the occurrences of a value in a 2D array or matrix.<\/p>\n Output:<\/strong><\/p>\n To count the occurrences of a value in each row of the 2D\u00a0 array we will pass the axis value as 1 in the count_nonzero() function.<\/p>\n It will return an array containing the count of occurrences of a value in each row.<\/p>\n\n
<\/a>Python numpy.count() Function<\/h2>\n
numpy.count()<\/code>in python aid in the counts for the non-overlapping occurrence of sub-string in the specified range. The syntax for phyton numpy.count() function is as follows:<\/p>\n
Syntax:<\/h3>\n
numpy.core.defchararray.count(arr, substring, start=0, end=None)<\/code><\/p>\n
Parameters:<\/h3>\n
\n
Returns:<\/h3>\n
<\/a>Example Code for numpy.count() Function in Python<\/h2>\n
# Python Program illustrating \r\n# numpy.char.count() method \r\nimport numpy as np \r\n \r\n# 2D array \r\narr = ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz']\r\n \r\nprint (\"arr : \", arr)\r\n \r\nprint (\"Count of 'Aa'\", np.char.count(arr, 'Aa'))\r\nprint (\"Count of 'Aa'\", np.char.count(arr, 'Aa', start = 8))<\/pre>\n
arr : ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz']\r\n\r\nCount of 'Aa' [1 1 1 1]\r\nCount of 'Aa' [1 0 0 0]<\/pre>\n
\n
<\/a>How to count the occurrences of a value in a NumPy array in Python<\/h2>\n
<\/a>Use count_nonzero()<\/h3>\n
count_nonzero()<\/code>function to count occurrences of a value in a NumPy array, which returns the count of values in a given numpy array. If the value of the axis argument is None, then it returns the count.<\/p>\n
import numpy as np\r\narr = np.array([9, 6, 7, 5, 6, 4, 5, 6, 5, 4, 7, 8, 6, 6, 7])\r\nprint('Numpy Array:')\r\nprint(arr)\r\n# Count occurrence of element '3' in numpy array\r\ncount = np.count_nonzero(arr == 6)\r\nprint('Total occurences of \"6\" in array: ', count)<\/pre>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nNumpy Array:\r\n[9 6 7 5 6 4 5 6 5 4 7 8 6 6 7]\r\nTotal occurences of \"6\" in array: 5\r\n\r\n<\/pre>\n
<\/a>Use sum()<\/h3>\n
sum()<\/code>function to count occurrences of a value in an array.<\/p>\n
import numpy as np\r\narr = np.array([9, 6, 7, 5, 6, 4, 5, 6, 5, 4, 7, 8, 6, 6, 7])\r\nprint('Numpy Array:')\r\nprint(arr)\r\n# Count occurrence of element '6' in numpy array\r\ncount = (arr == 6).sum()\r\n\r\nprint('Total occurences of \"6\" in array: ', count)<\/pre>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nNumpy Array: [9 6 7 5 6 4 5 6 5 4 7 8 6 6 7]\r\nTotal occurences of \"6\" in array: 5\r\n<\/pre>\n
<\/a>Use bincount()<\/h3>\n
bincount()<\/code>function to count occurrences of a value in an array.<\/p>\n
import numpy as np\r\narr = np.array([9, 6, 7, 5, 6, 4, 5, 6, 5, 4, 7, 8, 6, 6, 7])\r\ncount_arr = np.bincount(arr)\r\n# Count occurrence of element '6' in numpy array\r\nprint('Total occurences of \"6\" in array: ', count_arr[6])\r\n# Count occurrence of element '5' in numpy array\r\nprint('Total occurences of \"5\" in array: ', count_arr[5])<\/pre>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nTotal occurences of \"6\" in array: 5\r\nTotal occurences of \"5\" in array: 3\r\n<\/pre>\n
<\/a>Convert numpy array to list and count occurrences of a value in an array<\/h3>\n
count()<\/code>function on the list to get the count of occurrences of an element.<\/p>\n
import numpy as np\r\narr = np.array([9, 6, 7, 5, 6, 4, 5, 6, 5, 4, 7, 8, 6, 6, 7])\r\n# Count occurrence of element '6' in numpy array\r\ncount = arr.tolist().count(6)\r\nprint('Total occurences of \"6\" in array: ', count)<\/pre>\n
RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nTotal occurences of \"6\" in array:<\/pre>\n
<\/a>Select elements from the array that matches the value and count them<\/h3>\n
import numpy as np\r\n# Create a 2D Numpy Array from list of lists\r\nmatrix = np.array( [[2, 3, 4],\r\n [5, 3, 4],\r\n [5, 3, 5],\r\n [4, 7, 8],\r\n [3, 6, 2]] )\r\n# Count occurrence of element '3' in each column\r\ncount = np.count_nonzero(matrix == 3, axis=0)\r\nprint('Total occurrences of \"3\" in each column of 2D array: ', count)<\/pre>\n
Total occurrences of \"3\" in each column of 2D array: [1 3 0]<\/pre>\n
<\/a>Count occurrences of a value in 2D NumPy Array<\/h3>\n
import numpy as np\r\n# Create a 2D Numpy Array from list of lists\r\nmatrix = np.array( [[2, 3, 4],\r\n [5, 3, 4],\r\n [5, 6, 5],\r\n [6, 7, 6],\r\n [3, 6, 2]] )\r\n# Count occurrence of element '6' in complete 2D Numpy Array\r\ncount = np.count_nonzero(matrix == 6)\r\nprint('Total occurrences of \"6\" in 2D array:')\r\nprint(count)\r\n<\/pre>\n
Total occurrences of \"6\" in 2D array: 4<\/pre>\n
<\/a>Count occurrences of a value in each row of 2D NumPy Array<\/h3>\n
import numpy as np\r\n# Create a 2D Numpy Array from list of lists\r\nmatrix = np.array( [[2, 3, 4],\r\n [5, 3, 4],\r\n [6, 6, 5],\r\n [4, 7, 6],\r\n [3, 6, 2]] )\r\n# Count occurrence of element '6' in each row\r\ncount = np.count_nonzero(matrix == 6, axis=1)\r\nprint('Total occurrences of \"6\" in each row of 2D array: ', count)\r\n\r\n<\/pre>\n