[Numpy-discussion] Added atleast_nd, request for clarification/cleanup of atleast_3d

Nathaniel Smith njs at pobox.com
Wed Jul 6 12:35:24 EDT 2016

On Jul 6, 2016 6:12 AM, "Joseph Fox-Rabinovitz" <jfoxrabinovitz at gmail.com>
> I can add a keyword-only argument that lets you put the new dims
> before or after the existing ones. I am not sure how to specify
> arbitrary patterns for the new dimensions, but that should take care
> of most use cases.
> The use case that motivated this function in the first place is that I
> am doing some processing on 4D arrays and I need to reduce them but
> return a result with the original dimensionality (but not shape).
> atleast_nd seemed like a better solution than atleast_4d.

This is a tangent that might not apply given the details of your code, but
isn't this what keepdims is for? (And keepdims has the huge advantage that
it knows which axes are being reduced and thus where to put the new axes.)

I guess even if I couldn't use keepdims for some reason, my inclination
would be to try to emulate it by fixing up the axes as I went, because I'd
find it easier to verify that I hadn't accidentally misaligned things if
the reductions and fix-ups were local to each other, and explicit axis
insertions are much easier than trying to remember whether atleast_nd
prepends or appends. This of course is all based on some vague guess at
what your code actually looks like though...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160706/5d46c0ad/attachment.html>

More information about the NumPy-Discussion mailing list