In this article, we are going to learn about the All about View of a ndarray in NumPy. When we create an array in NumPy then it gets stored in a physical memory location. During our operations, we need the content which is stored at this memory location so we take a view of this content, which we call as View.
While working on the ndarray objects, we can share the same data among many objects.NumPy has ndarray.view() method which provides a new array object that looks like the same data of the original array. Also, we have a lot of NumPy functions that return a View of the array.
Though it looks the same and contains the same data as the Original array but important points to note here are :
- Any Change in dimensions of the new array doesn’t change the dimensions of the original array.
- The view is a shallow copy which means if you will make a change in the values of the array then the same changes will reflect in the original array as well.
Now let us understand this concept with some examples:
import numpy as np
#Now We will create two teams now as a view of players
players = np.array(["Sachin","Dhoni","Dravid","Kohli","Sourav","Yuvi"])
Team1 = players.view()
Team2 = players.view()
print(Team1)
print(Team2)
print(players)
Output:
['Sachin' 'Dhoni' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
['Sachin' 'Dhoni' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
['Sachin' 'Dhoni' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
From the output, you can see that Team1 and Team2 both are having the same contents. But they are not the same objects as players for NumPy. They are different object in itself. To validate this we can verify by using the id() function:
print("id for players Original array is : ",id(players))
print("id for View which is Team1 : ",id(Team1))
print("id for View which is Team2 : ",id(Team2))
Output:
id for players Original array is : 31838920
id for View which is Team1 : 31838800
id for View which is Team2 : 31839520
Also, we can confirm by using the ‘is’ operator of python that when we compare the Teams1 and Teams2 with the original array, it also confirms they are not the same.
print(Team1 is players)
print(Team2 is players)
Output:
False
False
Both results are False which confirms again that all three arrays are different, both Views and Original array.
Now let us make our understanding of the value changes. If we change any element in the Team1 or Team2, what will be the effect on original array players? Change a few elements of the Team2. It changes the elements of the players.
Here, we assign a new value to the first element of the Team2 array. You can see that the list of players has been “automatically” changed as well.
Team2[0] = "Rohit"
print(Team2)
print(players)
print(Team1)
Output:
['Rohit' 'Dhoni' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
['Rohit' 'Dhoni' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
['Rohit' 'Dhoni' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
Exactly same behaviour when we change an element of the Team1 array.
Team1[1] = "Sehwag"
print(Team1)
print(players)
print(Team2)
Output:
['Rohit' 'Sehwag' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
['Rohit' 'Sehwag' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
['Rohit' 'Sehwag' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
Now let us understand the dimensions change with the Views. So now we will change the shape of Team1 and let us see how it affects Team2 and the players original array.
Team1.shape = 3,2
print("Team1 new shape: ",Team1)
print("Team2 new shape: ",Team2)
print("Shape of players: ",players)
Output:
Team1 new shape: [['Rohit' 'Sehwag']
['Dravid' 'Kohli']
['Sourav' 'Yuvi']]
Team2 new shape: ['Rohit' 'Sehwag' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
Shape of players: ['Rohit' 'Sehwag' 'Dravid' 'Kohli' 'Sourav' 'Yuvi']
As you can see from the Output the shape of Team1 is changed to 3×2 and
to a Matrix. But original players array and Team2 is still same linear array.
There is no change in shape of original array and Team2.
Another important point about View is that you can check the origin of the view.So we can use the base property to verify that the view is a View of players array.
print(Team1.base is players)
print(Team2.base is players)
Output:
True
True