NumPy View and Copy

In this tutorial you will learn:

  • NumPy inbuilt copy function
  • NumPy inbuilt view function
  • NumPy View vs NumPy Copy

NumPy Copy

As the name indicates, the copy function copies the content of one array into another array. Hence, any changes in the copied array will not affect the original array (array from where the values have been copied). Copy function is a built in function and it can be used for various reasons within a program. In this example, I will demonstrate that how can we copy content of one array into another array with the help of copy function and in the output you can clearly observe that the changes in copied array will not affect the original array.

  1. import numpy as np
  2. my_arr = np.array(['a','b','c','d','e'])
  3. my_arr2=my_arr.copy()
  4. #now I will change the value of one of index of the copied array
  5. my_arr2[2]= 4
  6. print('The contents of original array are', my_arr, my_arr.dtype)
  7. print('The contents of copied array are', my_arr2, my_arr2.dtype)

Lets take another example of copying one array and making changes in both arrays to demonstrate no inter connection between copied and original array.

  1. import numpy as np
  2. my_arr = np.array([9,8,7,6,5,4,3])
  3. my_arr2=my_arr.copy()
  4. #now I will change the value of one of index of the copied array
  5. my_arr2[2]= 12
  6. print('The contents of original array are', my_arr, my_arr.dtype)
  7. print('The contents of copied array are', my_arr2, my_arr2.dtype)

NumPy View

In NumPy the original array is passed as a reference to another variable, whenever there is change in the view of the original array the original array actually gets changed. View function is a built in function and it can be used for various reasons within a program. In this example, I will demonstrate that how can we copy content of one array into another array with the help of view function and in the output you can clearly observe that the changes in copied array will affect the original array.

  1. import numpy as np
  2. my_arr = np.array(['z','y','x','w','v'])
  3. my_arr2=my_arr.view()
  4. #now I will change the value of one of index of the copied array
  5. my_arr2[2]= 4
  6. print('The contents of original array are', my_arr, my_arr.dtype)
  7. print('The contents of array using view function are', my_arr2, my_arr2.dtype)
This is another example demonstrating that any changes in the array copied by view() function will result changes in the original array.
  1. import numpy as np
  2. my_arr = np.array([100,200,300,400,500])
  3. my_arr2=my_arr.copy()
  4. #now I will change the value of one of index of the copied array
  5. my_arr2[2]= 22
  6. print('The contents of original array are', my_arr, my_arr.dtype)
  7. print('The contents of array using view function are', my_arr2, my_arr2.dtype)

Numpy View Vs Copy

NumPy view() and NumPy copy() both are builtin functions. But there is a very significant difference between NumPy View and NumPy copy as the same has already been demonstrated in the examples above. To further clarify any doubt we will have another example in which both the functions will be implied to a single array and their result will be displayed. Here you can see that the array being copied never affects the original array while the view array creates changes in the original array whenever it gets changed.

  1. import numpy as np
  2. print(' ** Copy Function **')
  3. arr = np.array(['s','o','u','r','c','e'])
  4. #arr2 will contain the copied values of original array(arr)
  5. arr2=arr.copy()
  6. print('Contents of original array without making any changes',arr)
  7. arr2[2]='a'
  8. print('Contents of original array after replacing index 2 in new array(made
  9. using copy function)', arr)
  10. print('** View Function **')
  11. #arr3 will contain the viewed values of original array(arr)
  12. arr3=arr.view()
  13. #Now I will change index 2 of arr 2 and arr 3
  14. print('Contents of original array without making any changes',arr)
  15. arr3[2]='a'
  16. print('Contents of original array after replacing index 2 in new array(made using view function)',arr2)

Tags

Add new comment