Python Numpy - Duplicate or Copy Array Data to Another Array
Python Numpy - Duplicate or Copy Array
You can copy a numpy array into another. Copying an array means that a new instance is created, and the elements of the original array are copied into the new array.
To copy array data to another using the Python Numpy library, you can use the numpy.ndarray.copy()
function.
Syntax
Following is the syntax to make a copy of a numpy array into another array:
array2 = array1.copy()
where array1
is a numpy n-dimensional array. array1.copy()
returns a new array but with the exact element values as that of array1
.
Examples
1. Copy Array using numpy.copy()
In the following example, we will copy the elements of an array a
to another array b
.
Python Program
import numpy as np
# create a numpy array
a = np.array([[8, 2, 3],
[4, 7, 6]])
# copy contents of a to b
b = a.copy()
# modify a
a[1, 2] = 13
# check if b has remained the same
print('a\n',a)
print('\nb\n',b)
Explanation:
- The array
a
is initialized as a 2D numpy array with two rows and three columns. - The
a.copy()
function creates a copy ofa
and stores it inb
. - We modify the element at index [1, 2] of
a
, but sinceb
is a separate copy, it remains unaffected.
Output
a
[[ 8 2 3]
[ 4 7 13]]
b
[[8 2 3]
[4 7 6]]
2. What happens if we use the assignment operator to copy an array?
This is a negative scenario. This example explains why you should use copy()
function instead of the assignment operator when you need to create a duplicate of an array.
Python Program
import numpy as np
# create a numpy array
a = np.array([[8, 2, 3],
[4, 7, 6]])
# assign a to b
b = a
# modify a
a[1, 2] = 13
# check if b has remained the same
print('a\n',a)
print('\nb\n',b)
Explanation:
- The array
a
is initialized as a 2D numpy array. - We assign
a
tob
using the assignment operator. Here,b
becomes a reference toa
. - When we modify the element at index [1, 2] of
a
,b
is also affected since both refer to the same underlying array.
Output
a
[[ 8 2 3]
[ 4 7 13]]
b
[[ 8 2 3]
[ 4 7 13]]
b
acts as a reference to a
. And when you change a
, then b
also gets changed. Hence, using the assignment operator is not a way to duplicate or copy a numpy array.
3. Copying a Multi-Dimensional Array
In this example, we will demonstrate how to copy a multi-dimensional array (like a 3D array) and modify its elements independently.
Python Program
import numpy as np
# create a 3D numpy array
arr = np.array([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
# copy contents of arr to another array
arr_copy = arr.copy()
# modify arr
arr[1, 0, 1] = 99
# print the original and copied arrays
print('Original Array: \n', arr)
print('Copied Array: \n', arr_copy)
Explanation:
- The array
arr
is initialized as a 3D numpy array with shape (2, 2, 2). - We create a copy of
arr
using thecopy()
function and assign it toarr_copy
. - We modify the element at index [1, 0, 1] in
arr
, but the copied arrayarr_copy
remains unchanged.
Output
Original Array:
[[[ 1 2]
[ 3 4]]
[[ 5 99]
[ 7 8]]]
Copied Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Summary
In this NumPy Tutorial of Python Examples, we learned how to copy a numpy array from one variable to another using the copy()
function. We also covered potential pitfalls when using the assignment operator and demonstrated how to copy multi-dimensional arrays while maintaining independence between the original and copied arrays.