[Numpy-discussion] ndarray.T2 for 2D transpose
Irvin Probst
irvin.probst at ensta-bretagne.fr
Thu Apr 7 03:39:14 EDT 2016
On 06/04/2016 04:11, Todd wrote:
>
> When you try to transpose a 1D array, it does nothing. This is the
> correct behavior, since it transposing a 1D array is meaningless.
> However, this can often lead to unexpected errors since this is rarely
> what you want. You can convert the array to 2D, using `np.atleast_2d`
> or `arr[None]`, but this makes simple linear algebra computations more
> difficult.
>
> I propose adding an argument to transpose, perhaps called `expand` or
> `expanddim`, which if `True` (it is `False` by default) will force the
> array to be at least 2D. A shortcut property, `ndarray.T2`, would be
> the same as `ndarray.transpose(True)`
>
Hello,
My two cents here, I've seen hundreds of people (literally hundreds)
stumbling on this .T trick with 1D vectors when they were trying to do
some linear algebra with numpy so at first I had the same feeling as
you. But the real issue was that *all* these people were coming from
matlab and expected numpy to behave the same way. Once the logic behind
1D vectors was explained it made sense to most of them and there were no
more problems.
And by the way I don't see any way to tell apart a 1D "row vector" from
a 1D "column vector", think of a code mixing a Rn=>R jacobian matrix and
some data supposed to be used as measurements in a linear system, so we
have J=np.array([1,2,3,4]) and B=np.array([5,6,7,8]), what would the
output of J.T2 and B.T2 be ?
I think it's much better to get used to writing
J=np.array([1,2,3,4]).reshape(1,4) and
B=np.array([5,6,7,8]).reshape(4,1), then you can use .T and @ without
any verbosity and at least if forces users (read "my students" here) to
think twice before writing some linear algebra nonsense.
Regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160407/825ec1d5/attachment.html>
More information about the NumPy-Discussion
mailing list