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

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Apr 7 13:46:23 EDT 2016


On Thu, Apr 7, 2016 at 1:35 PM, Sebastian Berg <sebastian at sipsolutions.net>
wrote:

> On Do, 2016-04-07 at 13:29 -0400, josef.pktd at gmail.com wrote:
> >
> >
> > On Thu, Apr 7, 2016 at 1:20 PM, Sebastian Berg <
> > sebastian at sipsolutions.net> wrote:
> > > On Do, 2016-04-07 at 11:56 -0400, josef.pktd at gmail.com wrote:
> > > >
> > > >
> > >
> > > <snip>
> > >
> > > >
> > > > 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`.
> > a.T2 @ b.T2.T
> >
>
> Actually, better would be:
>
>   a.T2 @ b.T2.T2  # Aha?
>
> And true enough, that works, but is it still reasonably easy to find
> and understand?
> Or is it just frickeling around, the same as you would try `a[:, None]`
> before finding `a[None, :]`, maybe worse?
>

I had thought about it earlier, but its "too cute" for my taste (and I
think I would complain during code review when I see this.)

Josef



>
> - Sebastian
>
> >
> > (T2 as shortcut for creating a[:, None] that's neat, except if a is
> > already 2D)
> >
> > Josef
> >
> > >
> > > 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
> > > pitfalls.
> > >
> > > - 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
> > >
> > > _______________________________________________
> > > 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
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160407/48199d0a/attachment.html>


More information about the NumPy-Discussion mailing list