Python – NumPy Array Copy

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 several ways 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 Python NumPy Array Copy

If you are in a hurry, below are some quick examples of how to get a Python NumPy array copy.


# Below are a quick example

# Example 1: Use numpy.copy() function
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
org_array = np.array([[58, 34, 28],[44, 88, 28]])
copy_array = org_array 

# Example 5: Creating an empty Numpy array
copy1 = np.empty_like(array)
copy1[:] = array

# Example 6: Use copy ones_like()
arr2 = np.ones_like(array)
print(arr2)

# Example 7: Use copy zeros_like()
arr2 = np.zeros_like(array)
print(arr2)

# Example 8: Use copy full_lice()
arr2 = np.full_like(array, 2)
print(arr2)

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 data
  • order – 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


import numpy as np
 
# Creating a numpy array using numpy.array()
array = np.array([4.54, 6.99, 8.42, 10.87, 16.94,
                      18.21, 22.65, 25.50, 31.5])
print("Original array: ")
print(array)
 
# Use numpy.copy() function
copy_array = np.copy(array)
print("\nCopied array: ")
print(copy_array)

Yields below output.


Original array: 
[ 4.54  6.99  8.42 10.87 16.94 18.21 22.65 25.5  31.5 ]

Copied array: 
[ 4.54  6.99  8.42 10.87 16.94 18.21 22.65 25.5  31.5 ]

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

Let’s see another example of the copy() function to copy multi-dimensional NumPy arrays. The below example demonstrates the copying of three-dimensional arrays.


import numpy as np

# Creating a 3-D numpy array using np.array()
array = np.array([[12,18,44], [66,37,89],[31,47,88]])
print("Original array: ")
print(array)
 
# using numpy.copy() function
copy_array = np.copy(array)
print("\nCopied array: ")
print(copy_array)

Yields below output.


Original array: 
[[12 18 44]
 [66 37 89]
 [31 47 88]]

Copied array: 
[[12 18 44]
 [66 37 89]
 [31 47 88]]

4. Use Assignment Operator

If you wanted to duplicate the array into another variable use assignment operatory to copy. When you used this approach, changing the values of one array will reflect the change on another array.


import numpy as np
 
# Use numpy.array() to Create a 2-D Numpy array
org_array = np.array([[58, 34, 28],
                      [44, 88, 28]])
# using Assignment operator
copy_array = org_array

# Let's change the value.
org_array[1, 2] = 77
print('Original Array: \n', org_array)
# printing copied array
print('\nCopied Array: \n', copy_array)

Yields below output.


Original Array: 
 [[58 34 28]
 [44 88 77]]

Copied Array: 
 [[58 34 28]
 [44 88 77]]

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
  
# Using np.array() to Creating a numpy array
array = np.array([0,3,5,7,9,11,13,15,17,19])
print("Original array: ")
print(array)
  
# Creating an empty Numpy array similar to ary
copy1 = np.empty_like(array)
# Now assign array to copy
copy1[:] = array
print("\nCopy of the given array: ")
print(copy1)

Yields below 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()

ones_like() – Returns the array with the same shape and type but fills with value one.


import numpy as np
 
# Creating a numpy array using numpy.array()
array = np.array([0,3,5,7,9,11,13,15,17,19])

# Use copy ones_like()
arr2 = np.ones_like(array)
print(arr2)

# OutPut
[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(arr2)

# OutPut
[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(arr2)

# OutPut
[2 2 2 2 2 2 2 2 2 2]

7. 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!!

You May Also Like

References

NumPy array copy

Leave a Reply

You are currently viewing Python – NumPy Array Copy