+4 votes
in Programming Languages by (74.2k points)
I want to find the count of positive and negative numbers in a big NumPy array. What function should I use?

E.g.

[-1, 0, 2, 3, -4, -5, 7, 8, -9]

positive count = 4

negative count = 4

1 Answer

+2 votes
by (349k points)
selected by
 
Best answer

You can use the sum() function as shown below to find the count of positive and negative numbers in an array.

>>> b=np.array([-1,0,2,3,-4,-5,7,8,-9])
>>> (b>0).sum()
4
>>> (b<0).sum()
4


...