Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to count occurences of a certain item in a numpy array

>>> import numpy as np
>>> y = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])

>>> np.count_nonzero(y == 1)
1
>>> np.count_nonzero(y == 2)
7
>>> np.count_nonzero(y == 3)
3
Comment

count occurrence in array python

arr = np.array( [1, 2, 3, 2, 1, 2])
occ = np.count_nonzero(arr==0)
print(occ)
0
occ = np.count_nonzero(arr==1)
print(occ)
2
Comment

How to count the occurrence of certain item in an ndarray?

a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
dict(zip(unique, counts))

# {0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
Comment

PREVIOUS NEXT
Code Example
Python :: rename a file in python 
Python :: plot path in pillow python 
Python :: python write subprocess stdout stderr to file 
Python :: print to file python 
Python :: clear variable jupyter notebook 
Python :: queue functions in python 
Python :: __mul__ 
Python :: array with zeros python 
Python :: python if not 
Python :: python json check if key exist 
Python :: spacy get number of tokens 
Python :: SUMOFPROD1 Solution 
Python :: pandas read csv file 
Python :: How to take multiple inputs in one line in python using split() 
Python :: get height of image in pygame 
Python :: k fold cross validation from scratch python 
Python :: create an empty array numpy 
Python :: python keyboard input arrow keys 
Python :: tuple in python 
Python :: python tableau 
Python :: python string to tuple 
Python :: send xml data with python 
Python :: how to find unique sublist in list in python 
Python :: python string replace letters with numbers 
Python :: if key not in dictionary python 
Python :: max and min int in python 
Python :: scan python 
Python :: writing to a file, with echo 
Python :: how to convert categorical data to numerical data in python 
Python :: matplotlib.pyplot 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =