NumPy arrays: how to remove extra axes
Tim Hochberg
tim.hochberg at ieee.org
Mon Jun 12 18:37:40 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).
> Thanks
>
>
> Huaiyu Zhu hzhu at knowledgetrack.com
Here's a function I just tossed together that should do what you
want. It's only barely tested, so I'd beat on it a bit before trusting
it.
-tim
--------------------------------------
from Numeric import *
def compactAxes(A):
"""Return an array with axes of length 1 removed"""
index = []
for length in shape(A):
if length == 1:
index.append(0)
else:
index.append(slice(None))
return A[tuple(index)]
--------------------------------------
More information about the Python-list
mailing list