Python Program on View vs Copy
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this article, we will explore a Python program that delves into the concepts of copying, referencing, and viewing arrays using the NumPy library. Understanding these concepts is crucial when working with arrays, as it directly impacts how changes to one array affect others. The program showcases different methods of creating copies, views, and references to NumPy arrays, providing insights into their behaviour and memory management.
Prerequisites
- Fundamental Python Knowledge (Variables, Data Types, Syntax)
- Basic Familiarity with the NumPy Library (Numerical Computing, Basic Array Operations)
Topic Explanation
This program initiates by importing the NumPy library as ‘np’ and generates an array using the ‘arange()’ function. It embarks on an exploration of multiple methods for copying arrays, encompassing ‘copy()’, ‘view()’, and straightforward reference assignment.
Within this exploration, the article delves into the distinctions between these copying techniques and elucidates how modifications made to one array can propagate to others. Readers gain a profound understanding of array copying mechanisms and their implications, enabling them to make informed choices when manipulating data using NumPy arrays.
Code:
import numpy as np
# Create an array using numpy.arange with values from 0 to 9
myar = np.arange(0,20, 3)
# Use numpy.logical_not to compute the element-wise logical NOT of the array
newar = np.logical_not(myar)
# Print the result of the logical NOT operation
print(newar)
# Create a copy of the original array using the copy() method
ar = myar.copy()
# Print the IDs of the original and copied arrays
print("Id of myarray ", id(myar))
print("Id of copy ", id(ar))
# Print the elements of the original and copied arrays
print("Elements of MYarray :", myar)
print("Elements of copied array :", ar)
# Modify an element in the original array
myar[2] = 500
# Print the elements of the arrays after the modification
print("-------------After Change----------")
print("Elements of Myarray :", myar)
print("Elements of copied array :", ar)
# Creates a view `ar` on `myar`
ar = myar.view()
# Print the IDs of the original array and its view
print("Id of myarray ", id(myar))
print("Id of view ", id(ar))
# Print the elements of the original array and its view
print("Elements of MYarray :", myar)
print("Elements of View :", ar)
# Modify an element in the original array
myar[2] = 500
# Print the elements of the arrays after the modification
print("-------------After Change----------")
print("Elements of MYarray :", myar)
print("Elements of View :", ar)
# Assign the reference of the original array to a new variable
ar = myar
# Modify elements in both the original and referenced arrays
ar[3] = 100
myar[6] = 500
# Print the elements of the arrays after the modification
print("-------------After Change----------")
print("My Array(myar): ", myar)
print("Reference(Ar): ", ar)
# Print the IDs of the original array and the reference
print("Id of myarray ", id(myar))
print("Id of reference ", id(ar))Output:
[ True False False False False False False]
Id of myarray 140674929030800
Id of copy 140674572829488
Elements of MYarray : [ 0 3 6 9 12 15 18]
Elements of copied array : [ 0 3 6 9 12 15 18]
————-After Change———-
Elements of Myarray : [ 0 3 500 9 12 15 18]
Elements of copied array : [ 0 3 6 9 12 15 18]
Id of myarray 140674929030800
Id of view 140674572829680
Elements of MYarray : [ 0 3 500 9 12 15 18]
Elements of View : [ 0 3 500 9 12 15 18]
————-After Change———-
Elements of MYarray : [ 0 3 500 9 12 15 18]
Elements of View : [ 0 3 500 9 12 15 18]
————-After Change———-
My Array(myar): [ 0 3 500 100 12 15 500]
Reference(Ar): [ 0 3 500 100 12 15 500]
Id of myarray 140674929030800
Id of reference 140674929030800
Code Explanation:
- Import numpy library and assign alias np
- Create ndarray myar with integers
- Create boolean ndarray newar that inverts truth value of myar
- Print newar
- Create ar as a copy of myar
- Print ids showing myar and ar have different ids
- Print elements showing myar and ar have same contents
- Modify myar by changing value at index 2 to 500
- Print updated myar and unchanged ar, showing copy was not affected
- Create ar as a view into myar
- Print ids showing view shares data with myar
- Print elements showing current identical contents
- Modify myar at index 2 to 500
- Print updated myar and also updated ar, showing changes reflected since ar views same data
- Make ar a reference to myar by assigning myar to ar
- Print ar showing it references same array contents
- Modify index 3 and 6 of ar and myar
- Print updated myar and ar, showing changes reflected in both since ar references myar
- Print ids confirming myar and ar reference same array
Summary
In summary, this Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework. By delving into the subtle nuances of how alterations in arrays affect copies, views, and references, it equips individuals with a profound comprehension of array behavior in Python.
This knowledge is not only pivotal for data manipulation but also crucial for ensuring data integrity and optimal performance in various computational tasks, making it an essential skill for both novice and experienced Python programmers alike.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

