View vs Copy¶
When the contents are physically stored in another location, it is called Copy.
On the other hand, a different view of the same memory content is provided, we call it as View.
Different array objects can share the same data. NumPy has ndarray.view() method which is a new array object that looks at the same data of the original array.
Here, change in dimensions of the new array doesn’t change dimensions of the original.
import numpy as np
fruits = np.array(["Apple","Mango","Grapes","Watermelon"])
We will create basket now as a view of fruits
basket_1 = fruits.view()
basket_2 = fruits.view()
print(basket_1)
print(basket_2)
['Apple' 'Mango' 'Grapes' 'Watermelon']
['Apple' 'Mango' 'Grapes' 'Watermelon']
print("ids for the arrays are different.")
print("id for fruits is : ")
print(id(fruits))
print("id for baskets is : ")
print(id(basket_1))
print(id(basket_2))
ids for the arrays are different.
id for fruits is :
4369559104
id for baskets is :
4369576544
4369576064
basket_1 is fruits
False
baskets is a view of the data owned by fruits¶
basket_1.base is fruits
True
Change a few elements of basket. It changes the elements of fruits¶
Here, we assign a new value to the first element of basket_2. You might be astonished that the list of fruits has been “automatically” changed as well. The explanation is that there has been no new assignment to basket_2, only to one of its elements.
basket_2[0] = "Strawberry"
basket_2
array(['Strawberry', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
fruits
array(['Strawberry', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
And this also affects basket_1¶
basket_1
array(['Strawberry', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
Change the entire elements of basket. It does not change fruits¶
basket_1 = np.array(["Peach","Pineapple","Banana","Orange"])
basket_1
array(['Peach', 'Pineapple', 'Banana', 'Orange'], dtype='<U9')
fruits
array(['Strawberry', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
In this case, a new memory location had been allocated for basket_1, because we have assigned a complete new list to this variable
Change the shape of basket. It does not change the shape of fruits¶
basket_2.shape = 2,2
print("basket_2: ")
print(basket_2)
basket_2:
[['Strawberry' 'Mango']
['Grapes' 'Watermelon']]
print("Shape of fruits: ")
print(fruits)
Shape of fruits:
['Strawberry' 'Mango' 'Grapes' 'Watermelon']
Slicing an array returns a view of it¶
mini_basket = fruits[2:]
mini_basket
array(['Grapes', 'Watermelon'], dtype='<U10')
fruits[3] = "Peach"
mini_basket
array(['Grapes', 'Peach'], dtype='<U10')
Deep Copy¶
The copy() method makes a complete copy of the array and its data, and doesn’t share with the original array.
import numpy as np
fruits = np.array(["Apple","Mango","Grapes","Watermelon"])
We now Create a deep copy of fruits
basket = fruits.copy()
basket
array(['Apple', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
basket is fruits
False
basket.base is fruits # basket doesn't share anything with fruits
False
Change contents or shape of bakset. It does not change the contents of fruits¶
basket [0] = "Strawberry"
basket
array(['Strawberry', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
fruits
array(['Apple', 'Mango', 'Grapes', 'Watermelon'], dtype='<U10')
basket.shape = 2,2
print("Shape of basket: ")
print(basket)
Shape of basket:
[['Strawberry' 'Mango']
['Grapes' 'Watermelon']]
print("Shape of fruits: ")
print(fruits)
Shape of fruits:
['Apple' 'Mango' 'Grapes' 'Watermelon']