From: Bill B. <wb...@gm...> - 2006-05-16 14:10:03
|
On 5/13/06, Tim Hochberg <tim...@co...> 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 =3D 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 wit= h a name like T is just asking for trouble. And defining it in every functio= n 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 ge= t 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 yo= u 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 o= r 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 |