+4 votes
in Programming Languages by (18.7k 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

0 votes
by (32.8k points)

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

...