Create one or more references to an existing ndarray:
- To create one are more references to an existing numpy.ndarray object the assignement operator can be used.
- Once another reference of an existing array object is created any change made to the array through new reference affects the original contents of the array.
- The following example creates three references to an existing numpy.ndarray object.
Example: Creating multiple references to an existing ndarray object
import numpy as np
# Create an ndarray object reference0 = np.arange(5) print("ndarray object printed through reference0:") print(reference0)
# Make two more references to the same ndarray object reference1 = reference2 = reference0
print("ndarray object printed through reference1:") print(reference1)
print("ndarray object printed through reference2:") print(reference2)
# Change ndarray contents through the reference1 reference1[0] = 10 print("ndarray contents changed through reference1 and printed through reference0:") print(reference0)
# Change the contents of the ndarray through the second reference2 reference2[1] = 20 print("ndarray contents changed through reference2 and printed through reference1:") print(reference1) |
- In the above example Python code, the references - reference0, reference1 and reference2 all point to the same ndarray object.
- Changes made through any of the references reflect in the original ndarray object.
Output:
ndarray object printed through reference0: [0 1 2 3 4] ndarray object printed through reference1: [0 1 2 3 4] ndarray object printed through reference2: [0 1 2 3 4] ndarray contents changed through reference1 and printed through reference0: [10 1 2 3 4] ndarray contents changed through reference2 and printed through reference1: [10 20 2 3 4] |
Create a deep copy of an ndarray object using : the slicing operator:
- A deep copy of ndarray elements can be created using the slicing operator. For this to work there should be an already existing target array with the same shape as the source array.
Example:
import numpy as np
# Define a class class Container: myVal = 0
def __init__(self, val): self.myVal = val
def __repr__(self): return "%d"%(self.myVal)
def __str__(self): return "%d"%(self.myVal)
# Create container instances c0 = Container(10) c1 = Container(20) c2 = Container(30)
# Create an ndarray of containers array1 = np.ndarray(3, dtype='object')
array1[0] = c0 array1[1] = c1 array1[2] = c2
print("array1 contents:") print(array1)
array2 = np.ndarray(3, dtype='object') array2[:] = array1
array2[0] = 1 array2[1] = 2 array2[2] = 3
print("array2 contents after deep copy and modifications to it:") print(array2)
print("array1 contents after deep copy and modifications to array2:") print(array1)
|
Output:
array1 contents: [10 20 30] array2 contents after deep copy and modifications to it: [1 2 3] array1 contents after deep copy and modifications to array2: [10 20 30] |
Create a deep copy of the numpy.ndarray object using the copy() method:
- The copy() method returns a deep copy of the numpy.ndarray object.
- While using the copy() method the memory layout can specified as any of the following using the parameter order:
- ’C’: ‘C’ Style or Row Major – meaning the row elements of the array are stored in subsequent array indices.
- ’F ’: Fortran Style or Column Major – meaning the column elements of the array are stored in subsequent array indices.
- ’A’: If the source array is Fortran style the same style will be applied. Else ‘C’ style will be applied.
- ’K ’: A memory layout that matches close to the memory layout of the source array.
Example:
import numpy as np
# Define a class class Container: myVal = 0
def __init__(self, val): self.myVal = val
def __repr__(self): return "%d"%(self.myVal)
def __str__(self): return "%d"%(self.myVal)
# Create Container instances c0 = Container(10) c1 = Container(20) c2 = Container(30)
# Create an ndarray of Containers array1 = np.ndarray(3, dtype='object')
array1[0] = c0 array1[1] = c1 array1[2] = c2
print("Array1 contents:") print(array1)
array2 = array1.copy()
print("array1:") print(array2)
print("array2:") print(array1)
array2[0] = 9 array2[1] = 8 array2[2] = 7
print("Array2 contents after modifying its elements:") print(array2)
print("Array1 contents after modifying array2:") print(array1) |
Output:
Array1 contents: [10 20 30] array1: [10 20 30] array2: [10 20 30] Array2 contents after modifying its elements: [9 8 7] Array1 contents after modifying array2: [10 20 30] |