[Numpy-discussion] reshaping empty array bug?

Nathaniel Smith njs at pobox.com
Tue Feb 23 15:30:41 EST 2016


On Tue, Feb 23, 2016 at 12:23 PM, Benjamin Root <ben.v.root at gmail.com> wrote:
>
> On Tue, Feb 23, 2016 at 3:14 PM, Nathaniel Smith <njs at pobox.com> wrote:
>>
>> Sure, it's totally ambiguous. These are all legal:
>
>
>
> I would argue that except for the first reshape, all of those should be an
> error, and that the current algorithm is buggy.

Reshape doesn't care about axes at all; all it cares about is that the
number of elements stay the same. E.g. this is also totally legal:

np.zeros((12, 5)).reshape((10, 3, 2))

And so are the equivalents

np.zeros((12, 5)).reshape((-1, 3, 2))
np.zeros((12, 5)).reshape((10, -1, 2))
np.zeros((12, 5)).reshape((10, 3, -1))

> This isn't a heuristic. It isn't guessing. It is making the semantics
> consistent. The fact that I can do:
> a.shape = (-1, 5, 64)
> or
> a.shape = (0, 5, 64)
>
> but not
> a.shape = (0, 5, -1)
>
> is totally inconsistent.

It's certainly annoying and unpleasant, but it follows inevitably from
the most natural way of defining the -1 semantics, so I'm not sure I'd
say "inconsistent" :-)

What should this do?

np.zeros((12, 0)).reshape((10, -1, 2))

-n

-- 
Nathaniel J. Smith -- https://vorpus.org



More information about the NumPy-Discussion mailing list