[Numpy-discussion] using reducing functions without eliminating dimensions?

Anne Archibald peridot.faceted at gmail.com
Thu Apr 9 02:29:49 EDT 2009


2009/4/9 Charles R Harris <charlesr.harris at gmail.com>:
>
>
> On Tue, Apr 7, 2009 at 12:44 PM, Dan Lenski <dlenski at gmail.com> wrote:
>>
>> Hi all,
>>
>> I often want to use some kind of dimension-reducing function (like min(),
>> max(), sum(), mean()) on an array without actually removing the last
>> dimension, so that I can then do operations broadcasting the reduced
>> array back to the size of the full array.  Full example:
>>
>>  >> table.shape
>>  (47, 1814)
>>
>>  >> table.min(axis=1).shape
>>  (47,)
>>
>>  >> table - table.min(axis=1)
>>  ValueError: shape mismatch: objects cannot be broadcast to a single
>> shape
>>
>>  >> table - table.min(axis=1)[:, newaxis]
>>
>> I have to resort to ugly code with lots of stuff like "... axis=1)[:,
>> newaxis]".
>>
>> Is there any way to get the reducing functions to leave a size-1 dummy
>> dimension in place, to make this easier?
>
> Not at the moment. There was talk a while back of adding a keyword for this
> option, it would certainly make things easier for some common uses. It might
> be worth starting that conversation up again.

What's wrong with np.amin(a,axis=-1)[...,np.newaxis]?

Anne

> Chuck
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>



More information about the NumPy-Discussion mailing list