In order to control precisely how memory is used, NumPy offers the concept of view of an array. Views are smaller arrays that share the same data as a larger array. This works just like a reference to one single object (refer to section Basic Types in Chapter 1, Getting Started).
The simplest example of a view is given by a slice of an array:
M = array([[1.,2.],[3.,4.]]) v = M[0,:] # first row of M
The preceding slice is a view of M
. It shares the same data as M
. Modifying v
will modify M
as well:
v[-1] = 0. v # array([[1.,0.]]) M # array([[1.,0.],[3.,4.]]) # M is modified as well
It is possible to access the object that owns the data using the array attribute base
:
v.base # array([[1.,0.],[3.,4.]]) v.base is M # True
If an array owns its data, the attribute base is none :
M.base # None