Use numpy.copy()
function to copy Python NumPy array (ndarray) to another array. This method takes the array you wanted to copy as an argument and returns an array copy of the given object. The copy owns the data and any changes made to the copy will not affect the original array. Alternatively, you can also try ndarray.copy()
function.
In this article, I will explain the Python numpy.copy()
array function syntax, parameters, and usage of how to copy Numpy array copy with examples by using functions like numpy.copy()
, numpy.empty_like()
, and assignment operator.
1. Quick Examples of Copy Array
If you are in a hurry, below are some quick examples of how to get a Python NumPy array copy.
# Quick examples of copy array
# Example 1: Use numpy.copy() function
array = np.array([4.54, 6.99, 8.42, 10.87, 16.94])
copy_array = np.copy(array)
# Example 2: Using numpy.array()
copy_array = np.array(array, copy=True)
# Example 3: Copy a multi-dimensional numpy array
array = np.array([[12,18,44],[66,37,89],[31,47,88]])
copy_array = np.copy(array)
# Example 4: Using assignment operator
# Updating copied array updates original array
original_array = np.array([5, 10, 15, 20,25])
copy_array = original_array
original_array[0] = 77
# Example 5: Creating an empty Numpy array
array = np.array([0,3,5,7,9,11,13,15,17,19])
copy1 = np.empty_like(array)
copy1[:] = array
# Example 6: Use copy ones_like()
array = np.array([0,3,5,7,9,11,13,15,17,19])
arr2 = np.ones_like(array)
# Example 7: Use copy zeros_like()
array = np.array([0,3,5,7,9,11,13,15,17,19])
arr2 = np.zeros_like(array)
# Example 8: Use copy full_lice()
array = np.array([0,3,5,7,9,11,13,15,17,19])
arr2 = np.full_like(array, 2)
2. Use NumPy.copy() Function
By using numpy.copy()
function you can create an array copy of the given object. In the below example, the given Numpy array ‘array‘
is copied to another array ‘copy_array‘
using copy()
function.
2.1 copy() Syntax
Following is the syntax of the numpy.copy() function.
# Use python numpy.copy() syntax
numpy.copy(arr, order='K', subok=False)
arr
– Input array dataorder
– Controls the memory layout of the copy.‘C’
means C-order,‘F’
means F-order,‘A’
means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. By default, it takes‘K’
value and that represents the layout of the array.subok
– If True, then sub-classes will be passed through, otherwise, the returned array will be forced to be a base-class array. This is an optional parameter and by default, it takes the ‘False’ value.
2.2 NumPy Array copy() Example
You can use the numpy.copy()
function to create a deep copy of a NumPy array. For instance, you create a NumPy array called array
. Then, you use the np.copy()
function to create a deep copy of the original array, which results in the copy_array
. The print()
statements display the original and copied arrays, showing that the copy_array
is indeed a copy of the array
. Any modifications made to copy_array
will not affect the array
.
# Import numpy
import numpy as np
# Creating a numpy array
array = np.array([4.54, 6.99, 8.42, 10.87, 16.94])
print("Original array:\n",array)
# Use numpy.copy() function
copy_array = np.copy(array)
print("After getting the copied array:\n",copy_array)
Yields below output.
Note: When you use copy()
, it copies the elements from the input array to another array hence, if you modify the original array the changes will not reflect on the copied array. If you use the assignment operator to copy then the values on both arrays are updated.
3. Copy Multi-Dimensional Array
You can also use the numpy.copy()
function to create a deep copy of a 3D NumPy array. For instance, you create a 3D NumPy array called array
. You then use the np.copy()
function to create a deep copy of the original 3D array, resulting in the copy_array
. The print()
statements display the original and copied 3D arrays, showing that copy_array
is indeed a copy of array
. Any modifications made to copy_array
will not affect the array
.
import numpy as np
# Creating a 3-D numpy array
array = np.array([[12,18,44], [66,37,89],[31,47,88]])
print("Original array:\n",array)
# Use numpy.copy() function
# Use multi-dimensional array
copy_array = np.copy(array)
print("After getting the copied multi-dimensional array:\n",copy_array)
Yields below output.
4. Use Assignment Operator
You can also create a copy of a NumPy array using the assignment operator =
. However, it’s important to note that the assignment operator creates a shallow copy, not a deep copy. A shallow copy means that the new array is a reference to the original array’s data. Changes made to the data through the copied array will affect the original array.
In the below example, changes made to the copy_array
using the assignment operator also affect the original_array
. If you want to create an independent copy of the array, it’s recommended to use the numpy.copy()
function or the .copy()
method of the NumPy array, both of which create deep copies.
import numpy as np
# Creating a NumPy array
original_array = np.array([5, 10, 15, 20,25])
print("Original array:\n",original_array)
# Using assignment operator
copy_array = original_array
# Let's change the value.
original_array[0] = 77
print("After getting the copied array:\n",copy_array)
Yields below output.
# Output:
Original array:
[ 5 10 15 20 25]
After getting the copied array:
[77 10 15 20 25]
5. Use NumPy.empty_like() Function
Use numpy.empty_like()
function to return a new array with the same shape and type as a given array. This function returns uninitialized (arbitrary) data with the same shape and type as a prototype. Since it doesn’t initialize the value, you need to assign the values to it to make a copy.
import numpy as np
# Creating a numpy array
array = np.array([0,3,5,7,9,11,13,15,17,19])
print("Original array:\n",array)
# Creating an empty Numpy array
# Similar to array
copy1 = np.empty_like(array)
# Now assign array to copy
copy1[:] = array
print("Copy of the given array:\n",copy1)
Yields below output.
# Output:
Original array:
[ 0 3 5 7 9 11 13 15 17 19]
Copy of the given array:
[ 0 3 5 7 9 11 13 15 17 19]
6. Copy ones_like(), zeros_like(), full_like()
np.ones_like()
creates a new array with the same shape and data type as the input array, but with all elements set to 1. For instance, arr2
is created using np.ones_like(array)
, and it produces a new array with the same shape as array
, but with all elements set to 1.
import numpy as np
# Creating a numpy array
array = np.array([0,3,5,7,9,11,13,15,17,19])
print("Original array:\n",array)
# Use copy ones_like()
arr2 = np.ones_like(array)
print("Copy of the given array:\n",arr2)
# Output:
# Original array:
# [ 0 3 5 7 9 11 13 15 17 19]
# Copy of the given array:
# [1 1 1 1 1 1 1 1 1 1]
zeros_like()
– Returns the array with the same shape and type but fills with values zero.
# Use copy zeros_like()
arr2 = np.zeros_like(array)
print("Copy of the given array:\n",arr2)
# Output:
# Copy of the given array:
# [0 0 0 0 0 0 0 0 0 0]
full_like()
– Returns the array with the same shape and type but fills with specified values.
# Use copy full_lice()
arr2 = np.full_like(array, 2)
print("Copy of the given array:\n",arr2)
# Output:
# Copy of the given array:
# [2 2 2 2 2 2 2 2 2 2]
Frequently Asked Questions
A shallow copy
creates a new array object, but it still references the original array’s data. Changes made in the shallow copy will affect the original array. In contrast, a deep copy
creates a completely independent copy with its own data. Changes made in a deep copy do not affect the original array.
Both numpy.copy()
function and ndarray.copy()
method creates deep copies of the original array. They create new array objects with their own data, independent of the original array. The usage syntax differs, but the result is the same: a new independent copy of the array.
You can create a shallow copy of a NumPy array using slicing: new_array = original_array[:]
. This method creates a new array that references the original array’s data.
NumPy provides functions like np.ones_like()
, np.zeros_like()
, and np.full_like()
to create new arrays with the same shape and data type as an existing array. np.ones_like()
creates an array filled with ones, np.zeros_like()
creates an array filled with zeros, and np.full_like()
creates an array filled with a specified constant value.
You can use the np.shares_memory()
function to check if two arrays share the same data. It returns True
if the arrays share the same data, and False
otherwise.
Conclusion
In this article, I have explained how to create a copy of the NumPy array using the numpy.copy()
function with examples and also covert what happens when you update the copied or original array.
Happy Learning!!
Related Articles
- How to Convert NumPy Array to List
- NumPy Element Wise Multiplication
- Python NumPy Interpolate Function
- Python NumPy Reverse Array
- NumPy Norm of Vector
- Python NumPy Square Root
- How to Convert NumPy Matrix to Array
- Python NumPy Absolute Value
- Python NumPy repeat() Function
- How to get square root values of an array?
References
- https://numpy.org/doc/stable/reference/generated/numpy.full_like.html#numpy.full_like
- https://numpy.org/doc/stable/reference/generated/numpy.empty_like.html#numpy.empty_like
Useful!