I have a 3D numpy array like a = np.zeros((100,100, 20))
. I want to perform an operation over every x,y
position that involves all the elements over the z
axis and the result is stored in an array like b = np.zeros((100,100))
on the same corresponding x,y
position.
Now i'm doing it using a for loop:
d_n = np.array([...]) # a parameter with the same shape as b
for (x,y), v in np.ndenumerate(b):
C = a[x,y,:]
### calculate some_value using C
minv = sys.maxint
depth = -1
C = a[x,y,:]
for d in range(len(C)):
e = 2.5 * float(math.pow(d_n[x,y] - d, 2)) + C[d] * 0.05
if e < minv:
minv = e
depth = d
some_value = depth
if depth == -1:
some_value = len(C) - 1
###
b[x,y] = some_value
The problem now is that this operation is much slower than others done the pythonic way, e.g. c = b * b
(I actually profiled this function and it's around 2 orders of magnitude slower than others using numpy built in functions and vectorized functions, over a similar number of elements)
How can I improve the performance of such kind of functions mapping a 3D array to a 2D one?
Many functions in Numpy are "reduction" functions*, for example
sum
,any
,std
, etc. If you supply anaxis
argument other thanNone
to such a function it will reduce the dimension of the array over that axis. For your code you can use theargmin
function, if you first calculatee
in a vectorized way:The indexing with
[...,None]
is used to engage broadcasting. The values ine
are floating point values, so it's a bit strange to compare tosys.maxint
but there you go:* Strickly speaking a reduction function is of the form
reduce(operator, sequence)
so technically notstd
andargmin
Obviously you want to get rid of the explicit
for
loop, but I think whether this is possible depends on what calculation you are doing with C. As a simple example,will fill the
100
by100
arrayb
with the sum of the squared "z" values ofa
, that is 1+4+9+...+400 = 2870.If your inner calculation is sufficiently complex, and not amenable to vectorization, then your iteration structure is good, and does not contribute significantly to the calculation time
There doesn't appear to be a special structure in the 1st 2 dimensions, so you could just as well think of it as 2D mapping on to 1D, e.g. mapping a
(N,20)
array onto a(N,)
array. That doesn't speed up anything, but may help highlight the essential structure of the problem.One step is to focus on speeding up that
C
tosome_value
calculation. There are functions likecumsum
andcumprod
that help you do sequential calculations on a vector.cython
is also a good tool.A different approach is to see if you can perform that internal calculation over the
N
values all at once. In other words, if you must iterate, it is better to do so over the smallest dimension.In a sense this a non-answer. But without full knowledge of how you get
some_value
fromC
andd_n
I don't think we can do more.It looks like
e
can be calculated for all points at once:On first glance it looks like this
E.argmin
is theb
value that you want (tweaked for some boundary conditions if needed).I don't have realistic
a
andd_n
arrays, but with simple test ones, thisE.argmin(-1)
matches yourb
, with a 66x speedup.What is usually done in 3D images is to swap the
Z
axis to the first index:And now you can easily iterate over the Z axis:
The
slice
here will be each of your100x100
fractions of your 3D matrix. Additionally, by transpossing allows you to access each of the 2D slices directly by indexing the first axis. For examplea[10]
will give you the 11th 2D100x100
slice.Bonus: If you store the data contiguosly, without transposing (or converting to a contiguous array using
a = np.ascontiguousarray(a.transpose((2,0,1)))
the access to you 2D slices will be faster since they are mapped contiguosly in memory.