Do you know how the NumPy count() function works? In this NumPy blog, I will explain what the np.count() function in Python is, its syntax, parameters required, and its return values with some illustrative examples.
The np.count() function in Python is a tool used for counting occurrences of a specific substring within each element of an array. Part of the NumPy library, this function works efficiently on arrays, including multi-dimensional ones, to find and count instances of a given word or character.
np.count() function in Python
The np.char.count() function in Python is part of the NumPy library, which is widely used for numerical computations. This function is specifically used for performing vectorized string operations for arrays of dtype numpy.str_ or numpy.unicode_.
It counts the number of occurrences of a substring in each element of an array.
np.count() Function Syntax
The np.count() function in Python syntax is as follows:
np.char.count(arr, sub, start=0, end=None)
NumPy count() Function Parameter
|arr||This is the array in which the function searches for the substring in Python.|
|sub||The substring to search for within each element of arr in Python.|
|start||The starting position in each element of arr in Python from which the search begins. The default is 0.|
|end||The ending position in each element of arr in Python up to which the search is performed. None means the search continues until the end of each element. The default is None.|
NumPy count() function in Python return values
The np.count() function in Python returns an array of integers, with each element representing the count of the substring sub in the corresponding element of the input array arr.
numpy.count() function in Python use cases
Let’s see some examples where we can learn the use of the np.count() function in Python.
1. NumPy count occurrences of all values in a Python array
In this example, the np.count() function in Python counts occurrences of the substring ‘hello’ in each element of the array arr.
import numpy as np arr = np.array(['apple pie', 'baseball game', 'American dream', 'statue of liberty']) sub = 'American' result = np.char.count(arr, sub) print(result)
[0 0 1 0]
The output from running the code in PyCharm is visually represented in the screenshot below.
2. np.count in Python with start and end parameter
Here, the np.count() function in Python searches for the substring between two positions in each element of the array.
import numpy as np arr = np.array(['Alabama', 'Alaska', 'California', 'Arizona']) sub = 'a' result = np.char.count(arr, sub, start=1, end=4) print(result)
[1 1 1 0]
Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.
3. numpy.count() function in 2D array
Here, we have to count the occurrence of the value in a 2D array in Python.
import numpy as np arr = np.array([['freedom of speech', 'freedom of expression'], ['freedom fighters', 'land of the free']]) sub = 'freedom' result = np.char.count(arr, sub) print(result)
[[1 1] [1 0]]
The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.
4. NumPy count occurrences of values in a 2D array with start and end parameters
let’s incorporate the start and end parameters in examples with 2D arrays using the np.char.count() function.
import numpy as np arr = np.array([['liberty and justice', 'pursuit of liberty'], ['statue of liberty', 'liberty bell']]) sub = 'liberty' start, end = 0, 10 result = np.char.count(arr, sub, start=start, end=end) print(result)
[[1 0] [0 1]]
After implementing the code in the Pycharm editor, the screenshot is mentioned below.
In this article, I have explained what the np.count() function in Python is in detail, how we can apply it, what the parameters required, and the return value. Also, different use cases in Python illustrate the np.char.count() function.
You may also like to read:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.