Efficient iteration over a 3D array?

I use Python and Numpy for data analysis.

I have a large 3D matrix (NxNxN), where each cell is again a matrix, this time a 3x3 matrix. Calling the matrix data, it looks like this:

data[N,N,N,3,3]  

I need to find the eigenvalues โ€‹โ€‹of all 3x3 matrices, and for this I use the Numpy procedure eigvals, but it takes a lot of time. Right now I'm pretty much doing this:

for i in range(N):
    for j in range(N):
        for k in range(N):
            a = np.linalg.eigvals(data[i,j,k,:,:])

At N = 256, this takes about an hour. Any ideas on how to make this more efficient?

Thanks so much for any suggestions!

+5
source share
3 answers

itertools.product , . , . , .

>>> bigdata = numpy.arange(256 * 256 * 256 * 3 * 3).reshape(256, 256, 256, 3, 3)
>>> %timeit numpy.linalg.eigvals(bigdata[100, 100, 100, :, :])
10000 loops, best of 3: 52.6 us per loop

:

>>> .000052 * 256 * 256 * 256 / 60
14.540253866666665

14 , . , ...

>>> def just_loops(N):
...     for i in xrange(N):
...         for j in xrange(N):
...             for k in xrange(N):
...                 pass
... 
>>> %timeit just_loops(256)
1 loops, best of 3: 350 ms per loop

, DSM. :

>>> def slice_loops(N, data):
...     for i in xrange(N):
...         for j in xrange(N):
...             for k in xrange(N):
...                 data[i, j, k, :, :]
... 
>>> %timeit slice_loops(256, bigdata)
1 loops, best of 3: 33.5 s per loop
+5

, NumPy, itertools.product , .

from itertools import product

for i, j, k in product(xrange(N), xrange(N), xrange(N)):
    a = np.linalg.eigvals(data[i,j,k,:,:])
+3

, , , .

+2

All Articles