The original tutorial may be found here.
When operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases:
import numpy as np
Simple assignments make no copy of array objects or of their data.
a = np.arange(12)
a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
b = a # no new object is created
b is a # a and b are two names for the same ndarray object
True
b.shape = 3, 4
b
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
a
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
Python passes mutable objects as references, so function calls make no copy.
def f(x):
print id(x)
id(a)
65508416L
f(a)
65508416
Different array objects can share the same data. The view method creates a new array object that looks at the same data.
c = a.view()
c is a
False
c.base is a # c is a view of the data owned by a
True
c.flags.owndata
False
c.shape = 2,6 # a's shape doesn't change
a
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
c
array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]])
c[0,4] = 1234 # a's data changes
a
array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])
c
array([[ 0, 1, 2, 3, 1234, 5], [ 6, 7, 8, 9, 10, 11]])
The copy method makes a complete copy of the array and its data.
d = a.copy() # a new array object with new data is created
d is a
False
d.base is a # d doesn't share anything with a
False
d[0,0] = 9999
a
array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])
d
array([[9999, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])
We have now completed the Tutorial Lesson 4 - Copies and Views