0

I'm trying to convert piece of matlab code to python.

a=[1 2 3;4 5 6]
b= sum(a<5)
//output :
ans :
2 1 1

Actually return the number of elements in every column which has the condition. Is there any equivalent function in numpy (python) to do this ?

1
  • 1
    Define the array properly, and sum along axis 0, (a<5).sum(axis=0) Commented May 1, 2019 at 12:27

2 Answers 2

1

Its the same.

a=np.array([[1, 2, 3],[4, 5, 6]])
b=np.sum(a<5,axis=0) # the only difference is that you need to explicitly set the dimension
Sign up to request clarification or add additional context in comments.

Comments

0

Although not made for this purpose, an alternate solution would be

a=np.array([[1, 2, 3],[4, 5, 6]])
np.count_nonzero(a<5, axis=0)
# array([2, 1, 1])

Performance

For small arrays, np.sum seems to be slightly faster

x = np.repeat([1, 2, 3], 100)
y = np.repeat([4, 5, 6], 100)
a=np.array([x,y])

%timeit np.sum(a<5, axis=0) 
# 7.18 µs ± 669 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit np.count_nonzero(a<5, axis=0)
# 11.8 µs ± 386 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

For very large arrays, np.count_nonzero seems to be slightly faster

x = np.repeat([1, 2, 3], 5000000)
y = np.repeat([4, 5, 6], 5000000)
a=np.array([x,y])

%timeit np.sum(a<5, axis=0) 
# 126 ms ± 6.92 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit np.count_nonzero(a<5, axis=0)
# 100 ms ± 6.72 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

2 Comments

That looks like a perfectly normal use for count_nonzero. For boolean array, this count and sum should produce the same result. I'd expect similar timings.
@hpaulj: Thanks for the comment. I have added some performance comparison.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.