[Numpy-discussion] calculating weighted majority using two 3D arrays

Gregory, Matthew matt.gregory@oregonstate....
Wed Mar 5 23:45:57 CST 2008

Hi list,

I'm a definite newbie to numpy, but finding the library to be incredibly

I'm trying to calculate a weighted majority using numpy functions.  I
have two sets of image stacks (one is values, the other weights) that I
read into 3D numpy arrays.  Assuming I read in a 100 row x 100 col image
subset consisting of ten images each, I have two arrays called values
and weights with the following shape:

values.shape = (10, 100, 100)
weights.shape = (10, 100, 100)

At this point I need to call my user-defined function to calculate the
weighted majority which should return a value for each 'pixel' in my 100
x 100 subset.  The way I'm doing it now (which I assume is NOT optimal)
is to pass values[:,i,j] and weights[:,i,j] to my function in a double
loop for i rows and j columns.  I then build up the return values into a
subsequent 2D array.

It seems like I should be able to use vectorize() or apply_along_axis()
to do this, but I'm not clever enough to figure this out.
Alternatively, should I be structuring my initial data differently so
that it's easier to use one of these functions.  The only way I can
think about doing that would be to store the two 10-item arrays into a
tuple and then make an array of these tuples, but that seemed overly
complicated.  Or potentially, is there a way to calculate a weighted
majority just using standard numpy functions??

Thanks for any suggestions,

More information about the Numpy-discussion mailing list