[Numpy-discussion] Suggestion: Add the unsqueeze function to numpy

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Apr 20 06:56:26 EDT 2011


On Wed, Apr 20, 2011 at 6:27 AM, Yannick Copin <y.copin at ipnl.in2p3.fr> wrote:
> Hi,
>
> I'm a very frequent user of the following "unsqueeze" function, which I
> initially copied from scipy/stats/models/robust/scale.py and which seems to be
> also present in scikits.statsmodels.tools.unsqueeze. Would it be possible to
> include it natively to numpy?
>
> def unsqueeze(data, axis, oldshape):
>     """
>     Unsqueeze a collapsed array
>
>     >>> from numpy import mean
>     >>> from numpy.random import standard_normal
>     >>> x = standard_normal((3,4,5))
>     >>> m = mean(x, axis=1)
>     >>> m.shape
>     (3, 5)
>     >>> m = unsqueeze(m, 1, x.shape)
>     >>> m.shape
>     (3, 1, 5)
>     """
>
>     if axis is None:
>         return data
>
>     newshape = list(oldshape)
>     newshape[axis] = 1
>     return data.reshape(newshape)

I also proposed this already once.

However there is already function in numpy  (where I have often
problems remembering the name):

numpy.expand_dims(a, axis)
Expand the shape of an array.
Insert a new axis, corresponding to a given position in the array shape.


Josef


>
> Cheers,
> --
>    .~.   Yannick COPIN  (o:>*  Doctus cum libro
>    /V\   Institut de physique nucléaire de Lyon
>   // \\  Université de Lyon - CNRS-IN2P3
>  /(   )\ AIM: YnCopin ICQ: 236931013
>   ^`~'^  http://snovae.in2p3.fr/ycopin/
>
>
> _______________________________________________
> 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