1

There are a few questions on SO about checking whether the numpy.reshape call has returned a copy or not [1, 2]. These questions are generally posed due to the vague warning from the doc that:

This will be a new view object if possible; otherwise, it will be a copy.

What I'm wondering is in what circumstances will NumPy return a copy? In every 2D reshape call I've tested, the method from user jterrace's answer in 2 shows that the memory base is the same (i.e. not a copy). Is it only for higher dimensional reshape that a copy may be necessary?

Additionally, the second part of the warning from the docs informs the user that:

...there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.

In other words, you may ask for row-major, but you could get column-major output. Doesn't that defeat the whole purpose of the order parameter? When would this case arise?

Community
  • 1
  • 1
marcman
  • 3,233
  • 4
  • 36
  • 71

1 Answers1

2

What I'm wondering is in what circumstances will NumPy return a copy?

In [13]: x = numpy.array([[1, 2, 3],
   ....:                  [4, 5, 6]])

In [14]: x[:, :2].reshape([4]).base is x
Out[14]: False

If the strides don't work for the new shape, NumPy has to copy.

In other words, you may ask for row-major, but you could get column-major output. Doesn't that defeat the whole purpose of the order parameter?

No. numpy.reshape provides no way to ask for a specific memory layout. The order parameter specifies what index order to read elements in; it has nothing to do with memory layout.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • In other words, in order to get the desired *memory layout*, one would need to explicitly write it to a copy in memory? – marcman Jun 20 '16 at 19:11
  • To elaborate, that last reshape produces `array([1, 2, 4, 5])`, a subset of the original `[1,2,3,4,5,6]`. – hpaulj Jun 20 '16 at 19:12
  • Okay. So am I to also understand that the only time a copy arises is when not all of the original data is retained through the reshape? As in this stride example – marcman Jun 20 '16 at 19:16
  • For the same `x`, `x.T.reshape(2,3)` is a copy. All the data is there, but it's in a different raveled order. – hpaulj Jun 20 '16 at 19:18
  • 2
    @marcman: No, that has nothing to do with it. A copy arises when the elements of the resulting array wouldn't have a consistent stride in memory along each axis for a view. – user2357112 Jun 20 '16 at 19:20