# [Numpy-discussion] Help making better use of numpy array functions

mdekauwe mdekauwe@gmail....
Thu Nov 26 07:49:18 CST 2009

```
Vincent Schut-2 wrote:
>
> Oh, and minor issue: creating a array of zeros and then multiplying with
> -999 still makes an array of zeros... I'd incorporated an array of
> *ones* multiplied with -999, because for the last chunk of days you
> could end up with a 8day array only partly filled with real data. E.g.
> if you'd have only 3 days of data left in your last chunk, 8dayData[0:3]
> would be data, and to prevent the rest ([3:8]) to be incorporated into
> the average calculation, these need to be -999. Currently these pixels
> will be 0, which is > nodatavalue, and thus infuencing your average (the
> pixelcount will be incremented for these 0 values).
>

Ok I hadn't thought about it in that way but you are of course right! I have
amended it.

Vincent Schut-2 wrote:
>
> Alternatively, you could simply take the sum over axis=0 of the weight
> array to get the pixel count (e.g. "pixelcount=weight.sum(axis=0)").
>

Ok I see your point here as well. So I tried implementing your suggestion,
as I understand it

weights = data8days > nodatavalue

will make and 8, nrows, ncols array containing true and false.

as you said I can get the pixel count I was after by using
weights.sum(axis=0).

However when I try to do the averaging step:

avg8days = np.average(data8days, axis=0, weights=weights)

I get the error msg " in average raise ZeroDivisionError, "Weights sum to
zero, can't be normalized"
ZeroDivisionError: Weights sum to zero, can't be normalized"

Which I guess (but don't know) comes from the trying do weight by a pixel
count of zero. So not sure what has happened here?

Thanks

--
View this message in context: http://old.nabble.com/Help-making-better-use-of-numpy-array-functions-tp26503657p26529681.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.

```