How do you count values in an array in python?

Onyejiaku Theophilus Chidalu

Overview

An array in Python is used to store multiple values of the same data type in a single variable.

The count[] method is used to return the number of occurrences of a value or item in an array.

Syntax

array.count[x]

x is the value we want to check for its number of occurrences.

Parameters

The parameter value of the count[] method is the value or element whose count we want to take in the given array.

Return value

The count[] method returns the number of occurrences of a value in an array.

Example

# importing the array module

import array as arr

# creating an integer data type array

x = arr.array['i', [1,2,3,4,5,6,1,2,3,2,4,7]]

# using the count function for the value 2 in the array

y = x.count[2]

# printing the number of counts

print['The number of occurrences of the value 2 in the array is: ', y]

Code explanation

  • Line 2: We import the array module.
  • Line 5: We use the array.array[] function to create an array variable x.
  • Line 8: We use the count[] function to take the count or number of occurrences of the number 2 in the array we created.
  • Line 11: We print the value of the occurrences.

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

If you are interested in the fastest execution, you know in advance which value[s] to look for, and your array is 1D, or you are otherwise interested in the result on the flattened array [in which case the input of the function should be np.ravel[arr] rather than just arr], then Numba is your friend:

import numba as nb


@nb.jit
def count_nb[arr, value]:
    result = 0
    for x in arr:
        if x == value:
            result += 1
    return result

or, for very large arrays where parallelization may be beneficial:

@nb.jit[parallel=True]
def count_nbp[arr, value]:
    result = 0
    for i in nb.prange[arr.size]:
        if arr[i] == value:
            result += 1
    return result

These can be benchmarked against np.count_nonzero[] [which also has a problem of creating a temporary array -- something that is avoided in the Numba solutions] and a np.unique[]-based solution [which is actually counting all unique value values contrarily to the other solutions].

import numpy as np


def count_np[arr, value]:
    return np.count_nonzero[arr == value]
import numpy as np


def count_np_uniq[arr, value]:
    uniques, counts = np.unique[a, return_counts=True]
    counter = dict[zip[uniques, counts]]
    return counter[value] if value in counter else 0 

Since the support for "typed" dicts in Numba, it is also possible to have a function counting all occurrences of all elements. This competes more directly with np.unique[] because it is capable of counting all values in a single run. Here is proposed a version which eventually only returns the number of elements for a single value [for comparison purposes, similarly to what is done in count_np_uniq[]]:

@nb.jit
def count_nb_dict[arr, value]:
    counter = {arr[0]: 1}
    for x in arr:
        if x not in counter:
            counter[x] = 1
        else:
            counter[x] += 1
    return counter[value] if value in counter else 0

The input is generated with:

def gen_input[n, a=0, b=100]:
    return np.random.randint[a, b, n]

The timings are reported in the following plots [the second row of plots is a zoom on the faster approaches]:

Showing that the simple Numba-based solution is fastest for smaller inputs and the parallelized version is fastest for larger inputs. They NumPy version is reasonably fast at all scales.

When one wants to count all values in an array, np.unique[] is more performant than a solution implemented manually with Numba for sufficiently large arrays.

EDIT: It seems that the NumPy solution has become faster in recent versions. In a previous iteration, the simple Numba solution was outperforming NumPy's approach for any input size.

Full code available here.

How do you count elements in an array in Python?

PROGRAM:.
#Initialize array..
arr = [1, 2, 3, 4, 5];.
#Number of elements present in an array can be found using len[].
print["Number of elements present in given array: " + str[len[arr]]];.

How do you count values in an array?

You can simply use the PHP count[] or sizeof[] function to get the number of elements or values in an array. The count[] and sizeof[] function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn't set.

How do you count values in a NumPy array in Python?

You can use the following methods to count the occurrences of elements in a NumPy array:.
Method 1: Count Occurrences of a Specific Value np. count_nonzero[x == 2].
Method 2: Count Occurrences of Values that Meet One Condition np. ... .
Method 3: Count Occurrences of Values that Meet One of Several Conditions np..

How do I count true values in NumPy array?

Use numpy. count_nonzero[] to count the number of True elements in a boolean array. Call numpy. count_nonzero[array] to return the number of True elements in a boolean array .

Chủ Đề