Reputation: 17617
It should be a standard question but I am not able find the answer :(
I have a numpy darray n samples (raw) and p variables (observation). I would like to count how many times each variables is non 0.
I would use a function like
sum([1 for i in column if i!=0])
but how can I apply this function to all the columns of my matrix?
Upvotes: 0
Views: 4731
Reputation: 67427
You can use np.sum
over a boolean array created from comparing your original array to zero, using the axis
keyword argument to indicate whether you want to count over rows or columns. In your case:
>>> a = np.array([[0, 1, 1, 0],[1, 1, 0, 0]])
>>> a
array([[0, 1, 1, 0],
[1, 1, 0, 0]])
>>> np.sum(a != 0, axis=0)
array([1, 2, 1, 0])
Upvotes: 1
Reputation: 6682
from this post: How to apply numpy.linalg.norm to each row of a matrix?
If the operation supports axis
, use the axis
parameter, it's usually faster,
Otherwise, np.apply_along_axis
could help.
Here is the numpy.count_nonzero
.
So here is the simple answer:
import numpy as np
arr = np.eye(3)
np.apply_along_axis(np.count_nonzero, 0, arr)
Upvotes: 2