NumPy arrays: how to remove extra axes

Konrad Hinsen hinsen at cnrs-orleans.fr
Tue Jun 13 06:14:05 EDT 2000


hzhu at rocket.knowledgetrack.com (Huaiyu Zhu) writes:

> For arrays in Numeric, is there a general way to remove axes of length 1?
> For example I have an array of shape (1,3,1,4) I want to change it to (3,4).

For a specific known shape, just assign a new one:

 array.shape = (3, 4)

For suppressing all axes of length one, no matter where, you can write

 array.shape = Numeric.repeat(array.shape, Numeric.not_equal(array.shape, 1))

Note that this changes the array, it does not create a new one.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------



More information about the Python-list mailing list