# [Numpy-discussion] add axis to results of reduction (mean, min, ...)

Robert Kern robert.kern@gmail....
Thu Aug 6 11:18:59 CDT 2009

```On Thu, Aug 6, 2009 at 11:15, Keith Goodman<kwgoodman@gmail.com> wrote:
> thanksOn Thu, Aug 6, 2009 at 9:07 AM, Robert Kern<robert.kern@gmail.com> wrote:
>> On Thu, Aug 6, 2009 at 11:03, Keith Goodman<kwgoodman@gmail.com> wrote:

>>>>> pylab.demean??
>>> Type:           function
>>> Base Class:     <type 'function'>
>>> String Form:    <function demean at 0x3c5c050>
>>> Namespace:      Interactive
>>> File:           /usr/lib/python2.6/dist-packages/matplotlib/mlab.py
>>> Definition:     pylab.demean(x, axis=0)
>>> Source:
>>> def demean(x, axis=0):
>>>    "Return x minus its mean along the specified axis"
>>>    x = np.asarray(x)
>>>    if axis:
>>>        ind = [slice(None)] * axis
>>>        ind.append(np.newaxis)
>>>        return x - x.mean(axis)[ind]
>>>    return x - x.mean(axis)
>>
>> Ouch! That doesn't handle axis=-1.
>>
>> if axis != 0:
>>    ind = [slice(None)] * x.ndim
>>    ind[axis] = np.newaxis
>
> Hey, didn't you warn us about the dangers of "if arr" the other day?

Yes, but actually that wasn't quite the problem. "if axis:" would have
been fine, if a bit obscure, as long as the body was fixed to not
expect axis>0.

--
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
```