[Numpy-discussion] Who uses matrix?

Bill Baxter wbaxter at gmail.com
Tue May 16 07:12:02 EDT 2006


On 5/13/06, Tim Hochberg <tim.hochberg at cox.net> wrote:
>
> Bill Baxter wrote:
>
> >
> > +1 on a .T shortcut for arrays.
> > +1 on a .H shortcut for arrays, too.  (Instead of .conj().transpose())
>
> -1.
>
> These are really only meaningul for 2D arrays. Without the axis keyword
> they generally do the wrong things for arrays of other dimensionality
> (there I usually, but not always, want to transpose the first two or
> last two axes). In addition, ndarray is already overstuffed with methods
> and attributes, let's not add any more without careful consideration.


What am I missing here?  There's already a .transpose() method on array.
>From my quick little test it doesn't seem like the argument is useful:
>>> a = num.rand(2,2)
>>> a
array([[ 0.96685836,  0.55643033],
       [ 0.86387107,  0.39331451]])
>>> a.transpose(1)
array([ 0.96685836,  0.55643033])
>>> a
array([[ 0.96685836,  0.55643033],
       [ 0.86387107,  0.39331451]])
>>> a.transpose(0)
array([ 0.96685836,  0.86387107])
>>> a.transpose()
array([[ 0.96685836,  0.86387107],
       [ 0.55643033,  0.39331451]])

(Python 2.4 / Windows XP)

But maybe I'm using it wrong.  The docstring isn't much help:
---------------------------
>>> help(a.transpose)
Help on built-in function transpose:

transpose(...)
    m.transpose(<None>)
---------------------------


Assuming I'm missing something about transpose(), that still doesn't rule
out a shorter functional form, like a.T() or a.t().

The T(a) that you suggest is short, but it's just not the order people are
used to seeing things in their math.  Besides, having a global function with
a name like T is just asking for trouble.  And defining it in every function
that uses it would get tedious.

Even if the .T shortcut were added as an attribute and not a function, you
could still use .transpose() for N-d arrays when the default two axes
weren't the ones you wanted to swap.  Yes, 2-D *is* a special case of array,
but -- correct me if I'm wrong -- it's a very common special case.  Matlab's
transpose operator, for instance, just raises an error if it's used on
anything other than a 1D- or 2D- array.  There's some other way to shuffle
axes around for N-d arrays in matlab, but I forget what.  Not saying that
the way Matlab does it is right, but my own experience reading and writing
code of various sorts is that 2D stuff is far more common than arbitrary
N-d.  But for some reason it seems like most people active on this mailing
list see things as just the opposite.  Perhaps it's just my little niche of
the world that is mostly 2D. The other thing is that I suspect you don't get
so many transposes when doing arbitrary N-d array stuff so there's not as
much need for a shortcut.  But again, not much experience with typical N-d
array usages here.



> > And some briefer way to make a new axis than 'numpy.newaxis' would be
> > nice too (I'm trying to keep my global namespace clean these days).
> > --- Whoa I just noticed that a[None,:] has the same effect as
> > 'newaxis'.  Is that a blessed way to do things?
>
> numpy.newaxis *is* None. I don't think that I'd spell it that way since
> None doesn't really have the same conotations as newaxis, so I think
> I'll stick with np.newaxis, which is how I spell these things.


Well, to a newbie, none of the notation used for indexing has much of any
connotation.  It's all just arbitrary symbols.  ":" means the whole range?
2:10 means a range from 2 to 9?  Sure, most of those are familiar to Python
users (even '...'?) but even a heavy python user would be puzzled by
something like r_[1:2:3j].  Or reshape(-1,2).  The j is a convention, the -1
is a convention.   NewAxis seems common enough to me that it's worth just
learning None as another numpy convention.

As long as "numpy.newaxis *is* None", as you say, and not "is *currently*
None, but subject to change without notice" , then I think I'd rather use
None.  It has the feel of being something that's a fundamental part of the
language.  The first few times I saw indexing with numpy.newaxis it made no
sense to me.  How can you index with a new axis?  What's the new axis'th
element of an array?  "None" says to me the index we're selecting is "None"
as in "None of the above", i.e. we're not taking from the elements that are
there, or not using up any of our current axes on this index.  Also when you
see None, it's clear to a Python user that there's some trickery going on,
but when you see  a[NewAxis,:] (as it was the first time I saw it) it's not
clear if NewAxis is some numeric value defined in the code you're reading or
by Numpy or what.  For whatever reason None seems more logical and
symmetrical to me than numpy.newaxis.   Plus it seems that documentation
can't be attached to numpy.newaxis because of it being None, which I recall
also confused me at first.  "help(numpy.newaxis)" gives you a welcome to
Python rather than information about using numpy.newaxis.

Regards,
--Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060516/96bed75d/attachment.html>


More information about the NumPy-Discussion mailing list