average of array containing NaN
I find myself in a situation where an array may contain not-Numbers that I set as NaN. Yet, whatever operation I do on that array( average, sum...) will threat the NaN as infinite values rather then ignoring them as I'd like it'd do. Am I missing something? Is this a bug or a feature? :-)
On 6/25/07, Giorgio F. Gilestro <giorgio@gilestro.tk> wrote:
Am I missing something? Is this a bug or a feature? :-) Neither. The best behaviour would probably be to throw an exception, but the extra checking that would require might well slow down other stuff. Try looking at the following functions, they should let you do what you want: 'nanargmax', 'nanargmin', 'nanmax', 'nanmin', 'nansum' _______________________________________________
-- . __ . |-\ . . tim.hochberg@ieee.org
On Monday 25 June 2007 14:15:20 Giorgio F. Gilestro wrote:
Giorgio, You could use: numpy.isnan(x).sum() But once again <push_product> masked arrays were designed to handle this kind of situation seamlessly. Just create a masked_array masked_array(x, mask=isnan(x)) and use the regular functions/methods on the masked array. </push_product>
Masked array seems definitely to be the way to go, thanks a lot. I must say that this entire issue doesn't make much sense to me: my understanding is the a NaN is different from an INF, therefore one would assume that really there is no reason why a not-number should not be ignored by default by all the array manipulating functions. On 6/25/07, Pierre GM <pgmdevlist@gmail.com> wrote:
Giorgio F. Gilestro wrote:
Because NaNs might also signal an error. Generally speaking, operations that involve NaNs should return NaNs. Operations that do something else with NaNs, like ignoring them, are special. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (6)
-
Andrew Straw
-
Charles R Harris
-
Giorgio F. Gilestro
-
Pierre GM
-
Robert Kern
-
Timothy Hochberg