<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 6, 2016 at 6:26 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class=""><p dir="ltr">On Jul 5, 2016 11:21 PM, "Ralf Gommers" <<a href="mailto:ralf.gommers@gmail.com" target="_blank">ralf.gommers@gmail.com</a>> wrote:<br>
><br>
><br>
><br>
> On Wed, Jul 6, 2016 at 7:06 AM, Nathaniel Smith <<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>> wrote:<br>
><br>
>> On Jul 5, 2016 9:09 PM, "Joseph Fox-Rabinovitz" <<a href="mailto:jfoxrabinovitz@gmail.com" target="_blank">jfoxrabinovitz@gmail.com</a>> wrote:<br>
>> ><br>
>> > Hi,<br>
>> ><br>
>> > I have generalized np.atleast_1d, np.atleast_2d, np.atleast_3d with a<br>
>> > function np.atleast_nd in PR#7804<br>
>> > (<a href="https://github.com/numpy/numpy/pull/7804" target="_blank">https://github.com/numpy/numpy/pull/7804</a>).<br>
>> ><br>
>> > As a result of this PR, I have a couple of questions about<br>
>> > `np.atleast_3d`. `np.atleast_3d` appears to do something weird with<br>
>> > the dimensions: If the input is 1D, it prepends and appends a size-1<br>
>> > dimension. If the input is 2D, it appends a size-1 dimension. This is<br>
>> > inconsistent with `np.atleast_2d`, which always prepends (as does<br>
>> > `np.atleast_nd`).<br>
>> ><br>
>> >   - Is there any reason for this behavior?<br>
>> >   - Can it be cleaned up (e.g., by reimplementing `np.atleast_3d` in<br>
>> > terms of `np.atleast_nd`, which is actually much simpler)? This would<br>
>> > be a slight API change since the output would not be exactly the same.<br>
>><br>
>> Changing atleast_3d seems likely to break a bunch of stuff...<br>
>><br>
>> Beyond that, I find it hard to have an opinion about the best design for these functions, because I don't think I've ever encountered a situation where they were actually what I wanted. I'm not a big fan of coercing dimensions in the first place, for the usual "refuse to guess" reasons. And then generally if I do want to coerce an array to another dimension, then I have some opinion about where the new dimensions should go, and/or I have some opinion about the minimum acceptable starting dimension, and/or I have a maximum dimension in mind. (E.g. "coerce 1d inputs into a column matrix; 0d or 3d inputs are an error" -- atleast_2d is zero-for-three on that requirements list.)<br>
>><br>
>> I don't know how typical I am in this. But it does make me wonder if the atleast_* functions act as an attractive nuisance, where new users take their presence as an implicit recommendation that they are actually a useful thing to reach for, even though they... aren't that. And maybe we should be recommending folk move away from them rather than trying to extend them further?<br>
>><br>
>> Or maybe they're totally useful and I'm just missing it. What's your use case that motivates atleast_nd?<br>
><br>
> I think you're just missing it:) atleast_1d/2d are used quite a bit in Scipy and Statsmodels (those are the only ones I checked), and in the large majority of cases it's the best thing to use there. There's a bunch of atleast_2d calls with a transpose appended because the input needs to be treated as columns instead of rows, but that's still efficient and readable enough.</p>
</span><p dir="ltr">I know people *use* it :-). What I'm confused about is in what situations you would invent it if it didn't exist. Can you point me to an example or two where it's "the best thing"? I actually had statsmodels in mind with my example of wanting the semantics "coerce 1d inputs into a column matrix; 0d or 3d inputs are an error". I'm surprised if there are places where you really want 0d arrays converted into 1x1,</p></blockquote><div>Scalar to shape (1,1) is less common, but 1-D to 2-D or scalar to shape (1,) is very common. Example is at the top of scipy/stats/stats.py: the _chk_asarray functions (used in many other functions) take care to never return scalar arrays because those are plain annoying to deal with. If that sounds weird to you, you're probably one of those people who was never surprised by this:<br><br>In [3]: x0 = np.array(1)<br><br>In [4]: x1 = np.array([1])<br><br>In [5]: x0[0]<br>---------------------------------------------------------------------------<br>IndexError                                Traceback (most recent call last)<br><ipython-input-5-6a57e371ca72> in <module>()<br>----> 1 x0[0]<br><br>IndexError: too many indices for array<br><br>In [6]: x1[0]<br>Out[6]: 1<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p dir="ltr"> or want to allow high dimensional arrays to pass through - and if you do want to allow high dimensional arrays to pass through, then transposing might help with 2d cases but will silently mangle high-d cases, right?</p></blockquote><div>>2d input handling is usually irrelevant. The vast majority of cases is "function that accepts scalar and 1-D array" or "function that accepts 1-D and 2-D arrays".<br></div><div> <br></div><div>Ralf<br><br></div></div><br></div></div>