[Numpy-discussion] ndarray.T2 for 2D transpose
Nathaniel Smith
njs at pobox.com
Wed Apr 6 17:20:51 EDT 2016
On Wed, Apr 6, 2016 at 10:43 AM, Todd <toddrjen at gmail.com> wrote:
> On Tue, Apr 5, 2016 at 11:14 PM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> On Tue, Apr 5, 2016 at 7:11 PM, Todd <toddrjen at gmail.com> 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)`.
>>
>> An alternative that was mentioned in the bug tracker
>> (https://github.com/numpy/numpy/issues/7495), possibly by me, would be
>> to have arr.T2 act as a stacked-transpose operator, i.e. treat an arr
>> with shape (..., n, m) as being a (...)-shaped stack of (n, m)
>> matrices, and transpose each of those matrices, so the output shape is
>> (..., m, n). And since this operation intrinsically acts on arrays
>> with shape (..., n, m) then trying to apply it to a 0d or 1d array
>> would be an error.
>>
>
> My intention was to make linear algebra operations easier in numpy. With
> the @ operator available, it is now very easy to do basic linear algebra on
> arrays without needing the matrix class. But getting an array into a state
> where you can use the @ operator effectively is currently pretty verbose and
> confusing. I was trying to find a way to make the @ operator more useful.
Can you elaborate on what you're doing that you find verbose and
confusing, maybe paste an example? I've never had any trouble like
this doing linear algebra with @ or dot (which have similar semantics
for 1d arrays), which is probably just because I've had different use
cases, but it's much easier to talk about these things with a concrete
example in front of us to put everyone on the same page.
-n
--
Nathaniel J. Smith -- https://vorpus.org
More information about the NumPy-Discussion
mailing list