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

Yannick Copin y.copin at ipnl.in2p3.fr
Wed Apr 20 06:27:09 EDT 2011


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/





More information about the NumPy-Discussion mailing list