I'm trying to understand when, after a reshape, numpy made a copy or a view. I was trying it analyzing the content of the base
attribute. I expected it to be None
when the array is a copy, the original array if it is a view. However, with the following code:
A = numpy.array([[1,2,20],[3,4,40],[5,6,60],[7,8,80],[9,10,100]])
print('A:\n',A)
print('A base:\n', A.base)
print('A initial shape:', A.shape)
B = A.reshape(3,5)
print('B:\n', B)
print('B base:\n', B.base)
C = A[1:3,0:2]
print('C:\n', C)
print('C base:\n', C.base)
D = C.reshape(4,1)
print('D:\n', D)
print('D base:\n', D.base)
I have the following output:
A:
[[ 1 2 20]
[ 3 4 40]
[ 5 6 60]
[ 7 8 80]
[ 9 10 100]]
A base:
None
A initial shape: (5, 3)
B:
[[ 1 2 20 3 4]
[ 40 5 6 60 7]
[ 8 80 9 10 100]]
B base:
[[ 1 2 20]
[ 3 4 40]
[ 5 6 60]
[ 7 8 80]
[ 9 10 100]]
C:
[[3 4]
[5 6]]
C base:
[[ 1 2 20]
[ 3 4 40]
[ 5 6 60]
[ 7 8 80]
[ 9 10 100]]
D:
[[3]
[4]
[5]
[6]]
D base:
[[3 4]
[5 6]]
I agree thatA
is raw array having base
attribute to None
, B
and C
are views of A
, so the base attribute points to the original A
array. However, I don't undestand the base
attribute of D
. I expected it is not a view but a new array, but the base
attribute point to a matrix [[3 4][5 6]]
(that is not C
, since C
is a view of A
, as shown in its base
attribute) instead of None
.
Why this? C
is a view of a new array never defined? Why C
is not simply the desired [[3] [4] [5] [6]]
array with None
in base
?