2

Assume the folowing numpy array:

[1,2,3,1,2,3,1,2,3,1,2,2] 

I want to count([1,2]) to count all occurrences of 1 and 2, in a single run, yielding something like

[4, 5] 

corresponding to a [1, 2] input.

Is it supported in numpy?

3
  • stackoverflow.com/a/28663910/4940954 Could you do something like this? This will give you the counts of each number. Commented Nov 27, 2019 at 20:55
  • yes, that's it. Thanks! Commented Nov 27, 2019 at 21:00
  • Use np.bincount(a)[[1, 2]] Commented Nov 27, 2019 at 21:08

1 Answer 1

1
# Setting your input to an array array = np.array([1,2,3,1,2,3,1,2,3,1,2,2]) # Find the unique elements and get their counts unique, counts = np.unique(array, return_counts=True) # Setting the numbers to get counts for as a set search = {1, 2} # Gets the counts for the elements in search search_counts = [counts[i] for i, x in enumerate(unique) if x in search] 

This will output [4, 5]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.