Tips on how to Rely Occurrences of Parts in NumPy


You’ll be able to significance refer to modes to rely the occurrences of components in a NumPy array:

Mode 1: Rely Occurrences of a Explicit Price

np.count_nonzero(x == 2)

Mode 2: Rely Occurrences of Values that Meet One Status

np.count_nonzero(x < 6)

Mode 3: Rely Occurrences of Values that Meet Certainly one of A number of Statuses

np.count_nonzero((x == 2) | (x == 7))

Refer to examples display find out how to significance each and every form in observe with refer to NumPy array:

import numpy as np

#form NumPy array
x = np.array([2, 2, 2, 4, 5, 5, 5, 7, 8, 8, 10, 12])

Instance 1: Rely Occurrences of a Explicit Price

Refer to code displays find out how to rely the selection of components within the NumPy array which can be equivalent to the worth 2:

#rely selection of values in array equivalent to two
np.count_nonzero(x == 2)

3

From the output we will be able to see that 3 values within the NumPy array are equivalent to two.

Instance 2: Rely Occurrences of Values that Meet One Status

Refer to code displays find out how to rely the selection of components within the NumPy array that experience a price lower than 6:

#rely selection of values in array which can be lower than 6
np.count_nonzero(x < 6)

7

From the output we will be able to see that 7 values within the NumPy array have a price lower than 6.

Instance 3: Rely Occurrences of Values that Meet Certainly one of A number of Statuses

Refer to code displays find out how to rely the selection of components within the NumPy array which can be equivalent to two or 7:

#rely selection of values in array which can be equivalent to two or 7
np.count_nonzero((x == 2) | (x == 7))

4

From the output we will be able to see that 4 values within the NumPy array are equivalent to two or 7.

Spare Sources

Refer to tutorials give an explanation for find out how to carry out alternative familiar operations in Python:

Tips on how to Calculate the Method of NumPy Array
Tips on how to Map a Serve as Over a NumPy Array
Tips on how to Type a NumPy Array through Column

Leave a Comment