[Numpy-discussion] ndarray.T2 for 2D transpose

Sebastian Berg sebastian at sipsolutions.net
Thu Apr 7 13:20:24 EDT 2016

On Do, 2016-04-07 at 11:56 -0400, josef.pktd at gmail.com wrote:


> I don't think numpy treats 1d arrays as row vectors. numpy has C
> -order for axis preference which coincides in many cases with row
> vector behavior.

Well, broadcasting rules, are that (n,) should typically behave similar
to (1, n). However, for dot/matmul and @ the rules are stretched to
mean "the one dimensional thing that gives an inner product" (using
matmul since my python has no @ yet):

In [12]: a = np.arange(20)
In [13]: b = np.arange(20)

In [14]: np.matmul(a, b)
Out[14]: 2470

In [15]: np.matmul(a, b[:, None])
Out[15]: array([2470])

In [16]: np.matmul(a[None, :], b)
Out[16]: array([2470])

In [17]: np.matmul(a[None, :], b[:, None])
Out[17]: array([[2470]])

which indeed gives us a fun thing, because if you look at the last
line, the outer product equivalent would be:

    outer = np.matmul(a[None, :].T, b[:, None].T)

Now if I go back to the earlier example:

    a.T @ b

Does not achieve the outer product at all with using T2, since

    a.T2 @ b.T2  # only correct for a, but not for b
    a.T2 @ b  # b attempts to be "inner", so does not work

It almost seems to me that the example is a counter example, because on
first sight the `T2` attribute would still leave you with no shorthand
for `b`.

I understand the pain of having to write (and parse get into the depth
of) things like `arr[:, np.newaxis]` or reshape. I also understand the
idea of a shorthand for vectorized matrix operations. That is, an
argument for a T2 attribute which errors on 1D arrays (not sure I like
it, but that is a different issue).

However, it seems that implicit adding of an axis which only works half
the time does not help too much? I have to admit I don't write these
things too much, but I wonder if it would not help more if we just
provided some better information/link to longer examples in the
"dimension mismatch" error message?

In the end it is quite simple, as Nathaniel, I think I would like to
see some example code, where the code obviously looks easier then
before? With the `@` operator that was the case, with the "dimension
adding logic" I am not so sure, plus it seems it may add other

- Sebastian

> >>> np.concatenate(([[1,2,3]], [4,5,6]))
> Traceback (most recent call last):
>   File "<pyshell#63>", line 1, in <module>
>     np.concatenate(([[1,2,3]], [4,5,6]))
> ValueError: arrays must have same number of dimensions
> It's not an uncommon exception for me.
> Josef
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > https://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160407/47cfd91f/attachment.sig>

More information about the NumPy-Discussion mailing list