drjrm3
drjrm3

Reputation: 4718

Efficient way to create an array of nonzero values in a numpy tensor?

I have a 3D numpy grid A[ix,iy,iz] and I filter out elements by zeroing them out via A[ A<minval ] = 0, or A[ A>maxval ] = 0, etc. I then want to perform statistics on the remaining items. For now, I am doing:

for ai in np.reshape(A, nx*ny*nz):
    if( ai > 0 ):
        Atemp.append(ai)

and then I perform statistics on Atemp. This takes quite a long time, however, and I wonder if there is a more efficient way to create Atemp. For what it's worth, I am working with several GB of data in these arrays.

NOTE: I do not want a different way to filter these items out. I want to zero them out, then create a temporary array of all nonzero elements in A.

Upvotes: 1

Views: 891

Answers (2)

EelkeSpaak
EelkeSpaak

Reputation: 2825

Another option:

 Atemp = A.ravel()[np.flatnonzero(A)]

Upvotes: 0

David Wolever
David Wolever

Reputation: 154484

You can use:

Atemp = A[A != 0]

Eg:

In [3]: x = np.array([0,1,2,3,0,1,2,3,0]).reshape((3,3))

In [4]: x
Out[4]: 
array([[0, 1, 2],
       [3, 0, 1],
       [2, 3, 0]])

In [5]: x[x == 0]
Out[5]: array([0, 0, 0])

In [6]: x[x != 0]
Out[6]: array([1, 2, 3, 1, 2, 3])

Upvotes: 4

Related Questions