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

numpy count occurrences in interval array

# credit to Stack Overflow user in source link
# a is a numpy array
# Note: the following syntax allows you to count the number of elements x
# of the array such that 25 < x < 100 
((a > 25) & (a < 100)).sum()
Comment

PREVIOUS NEXT
Code Example
Python :: extract pdf with python 
Python :: python print color 
Python :: remove extra spaces python 
Python :: close python window after execution 
Python :: print groupby dataframe 
Python :: Plot regression line from sklearn 
Python :: how to find the datatype of a dataframe in python 
Python :: twin axis python 
Python :: pygame how to draw a rectangle 
Python :: pandas backfill 
Python :: pandas dataframe crosstab 
Python :: slicing string in python 
Python :: python iterate through files in directory 
Python :: Get Current Date using today method 
Python :: python convert string to sentence case 
Python :: elif in django template 
Python :: PIL image example 
Python :: commentaire python 
Python :: subprocess.check_output python 
Python :: pickle.dump python 
Python :: python checking if something is equal to NaN 
Python :: pandas column name equal to another column value 
Python :: access google transalte pandas 
Python :: tkinter margin 
Python :: drop all characters after a character in python 
Python :: change matplotlib fontsize 
Python :: multiprocessing queue python 
Python :: make the program title a name python 
Python :: python data structures 9.4 
Python :: pandas replace values based on condition 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =