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

numpy count occurrences in array

unique, counts = numpy.unique(a, return_counts=True)
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 :: train test split python 
Python :: making hexagon in python turtle 
Python :: add element to heap python 
Python :: get number of bits on integer in python 
Python :: get dictionary in array python by value 
Python :: exit python script 
Python :: bulk file name changer in python 
Python :: pandas read ods 
Python :: python scratch cloud variabelen 
Python :: python open file same folder 
Python :: tqdm in python 
Python :: key item loop list python 
Python :: matplotlib bold 
Python :: # find the common elements in the list. 
Python :: kaaba python tutorial 
Python :: change the style of notebook tkinter 
Python :: django text area limit characters 
Python :: import static in django urls 
Python :: python write list to text file 
Python :: pandas convert all string columns to lowercase 
Python :: how to say hello world 
Python :: flask for loops 
Python :: python sum attribute in list 
Python :: jupyter themes 
Python :: pandas read excel nan 
Python :: django not saving images forms 
Python :: random with probability python 
Python :: how to move a column to last in pandas 
Python :: tf.contrib.layers.xavier_initializer() tf2 
Python :: django.core.exceptions.FieldError: Unknown field(s) (author) specified for Comment 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =