[PYTHON MATRIX-SIG] Newbie: transpose

David Ascher da@maigret.cog.brown.edu
Thu, 20 Mar 1997 20:23:53 -0500 (EST)


> Excuse me if the following has been discussed extensively (if so, could
> somebody point me to the relevant archive messages?): To me
> transpose() seems to act counter-intuitive:

I'll go through your examples in detail, but have you looked at the
tutorial?  It is out of date, I realize, but this sort of stuff is
covered, at least superficially.  If you haven't, I'd love it for you (and
other newbies) to give it a look and suggest improvements.  I *will* work
on it, but not this month...

> Doing:
> 
> >>> b
> array([3, 4, 5])
> >>> transpose(b)
> array([3, 4, 5])
> >>>
> 
> I expected:
> 
> >>> transpose(b)
> array([[3],
>        [4],
>        [5]])
> >>>

>From the tutorial:

transpose() takes an array and returns a new array which corresponds to a
with the order of axes specified by the second argument. The default
corresponds to flipping the order of all the axes (thus it is equivalent
to a.shape[::-1]).

Now the key to understanding why that didn't do what you expected is that
the shape of your array b = array([3,4,5]) is (3,) -- it is a one-tuple --
meaning that it has only one axis (of dimension 3), which can't be swapped
with any other axis.  So transpose() doesn't do anything.

Similarly:

> b = b[:,NewAxis]
> >>> b
> array([[3],
>        [4],
>        [5]])

Now the shape of b is (3,1), so

> >>> c = transpose(b)
> >>> c
> array([       [3, 4, 5]])
> >>> c.shape
> (1, 3)

makes sense as well given the above definition.

> I expected:
> 
> >>> c = transpose(b)
> >>> c
> array([3, 4, 5])
> >>> c.shape
> (3,)

Transpose won't change the rank (number of dimensions, length of shape
tuple), so the only way to go from an array of shape (1,3) to one of shape
(3,) is to slice it.

I hope this helps.  One big issue when learning Numeric is that it is not
MATLAB -- in other words, because it is designed to deal with arrays of
arbitrary rank (and not just 2D matrices), some of the assumptions which
make sense in a linear algebra context don't transfer all that well.  The
other issue I've had is to learn to assimilate what choose, where, etc.
all do and not mix them up.  That comes with practice.

--david


_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________