Suggestion: Add the unsqueeze function to numpy

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) 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/

On Wed, Apr 20, 2011 at 6:27 AM, Yannick Copin <y.copin@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@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

<josef.pktd <at> gmail.com> writes:
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)
Ah, thanks for the tip, I didn't know this one. The name is unfortunate indeed... Cheers, Yannick

You can also insert new axes when you slice an array via np.newaxis, fwiw:
import numpy as np x = np.random.random((3,4,5)) y = x.mean(axis=1) y.shape (3, 5) y[:,np.newaxis,:].shape (3, 1, 5)
-- Dan Lepage On Wed, Apr 20, 2011 at 1:24 PM, Yannick Copin <yannick.copin@laposte.net> wrote:
<josef.pktd <at> gmail.com> writes:
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)
Ah, thanks for the tip, I didn't know this one. The name is unfortunate indeed...
Cheers,
Yannick
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Wed, Apr 20, 2011 at 2:19 PM, Daniel Lepage <dplepage@gmail.com> wrote:
You can also insert new axes when you slice an array via np.newaxis, fwiw:
import numpy as np x = np.random.random((3,4,5)) y = x.mean(axis=1) y.shape (3, 5) y[:,np.newaxis,:].shape (3, 1, 5)
That's convenient if you only have a specific axis to take care of, but I like expand_dims when I write functions that are supposed to work with any axis. def demean(x, axis=0): return x - np.expand_dims(x.mean(axis), axis) or something like this Josef
-- Dan Lepage
On Wed, Apr 20, 2011 at 1:24 PM, Yannick Copin <yannick.copin@laposte.net> wrote:
<josef.pktd <at> gmail.com> writes:
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)
Ah, thanks for the tip, I didn't know this one. The name is unfortunate indeed...
Cheers,
Yannick
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On 04/20/2011 12:24 PM, Yannick Copin wrote:
<josef.pktd<at> gmail.com> writes:
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) Ah, thanks for the tip, I didn't know this one. The name is unfortunate indeed...
Cheers,
Yannick
If you don't like the name, you can propose a different name via ticket (probably less likely but numpy 2.0 is around the corner). In the immediate term, please find time to update the documentation so both squeeze and expand_dim are added to the respective pages so that it easier to find - an joint example of squeeze/expand_dim would help here. Bruce
participants (5)
-
Bruce Southey
-
Daniel Lepage
-
josef.pktd@gmail.com
-
Yannick Copin
-
Yannick Copin