May 2019
Beginner
528 pages
29h 51m
English
The previous chapter introduced view objects—that is, objects that “see” the data in other objects, rather than having their own copies of the data. Views are also known as shallow copies. Various array methods and slicing operations produce views of an array’s data.
The array method view returns a new array object with a view of the original array object’s data. First, let’s create an array and a view of that array:
In [1]: import numpy as npIn [2]: numbers = np.arange(1, 6)In [3]: numbersOut[3]: array([1, 2, 3, 4, 5])In [4]: numbers2 = numbers.view()In [5]: numbers2Out[5]: array([1, 2, 3, 4, 5])
We can use the built-in id function to see that numbers and numbers2 are different objects:
In [6]: id(numbers) ...Read now
Unlock full access