Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Copy NumPy array into another array?

  • Last Updated : 05 Sep, 2020

Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. There are 3 methods to copy a Numpy array to another array.

Method 1: Using np.empty_like() function

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

This function returns a new array with the same shape and type as a given array.



Syntax:

numpy.empty_like(a, dtype = None, order = ‘K’, subok = True)

Python3




# importing Numpy package
import numpy as np
  
# Creating a numpy array using np.array()
ary = np.array([13, 99, 100, 34, 65, 11
                66, 81, 632, 44])
  
print("Original array: ")
  
# printing the Numpy array
print(ary)
  
# Creating an empty Numpy array similar
# to ary
copy = np.empty_like(ary)
  
# Now assign ary to copy
copy[:] = ary
  
print("\nCopy of the given array: ")
  
# printing the copied array
print(copy)

Output:

In the above example, the given Numpy array ‘ary‘ is copied to another array ‘copy‘ using np.empty_like () function

Method 2: Using np.copy() function

This function returns an array copy of the given object.

Syntax :



numpy.copy(a, order='K', subok=False)

Example 1:

Python3




# importing Numpy package
import numpy as np
  
# Creating a numpy array using np.array()
org_array = np.array([1.54, 2.99, 3.42, 4.87, 6.94,
                      8.21, 7.65, 10.50, 77.5])
  
print("Original array: ")
  
# printing the Numpy array
print(org_array)
  
# Now copying the org_array to copy_array
# using np.copy() function
copy_array = np.copy(org_array)
  
print("\nCopied array: ")
  
# printing the copied Numpy array
print(copy_array)

Output:

In the above example, the given Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using np.copy () function

Example 2: Copy given 3-D array to another array using np.copy() function

Python3




# importing Numpy package
import numpy as np
  
# Creating a 3-D numpy array using np.array()
org_array = np.array([[23, 46, 85],
                      [43, 56, 99],
                      [11, 34, 55]])
  
print("Original array: ")
  
# printing the Numpy array
print(org_array)
  
# Now copying the org_array to copy_array
# using np.copy() function
copy_array = np.copy(org_array)
  
print("\nCopied array: ")
  
# printing the copied Numpy array
print(copy_array)

Output:

In the above example, the given 3-D Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using np.copy () function

Method 3: Using Assignment Operator

Python3




# importing Numpy package
import numpy as np
  
# Create a 2-D Numpy array using np.array()
org_array = np.array([[99, 22, 33],
                      [44, 77, 66]])
  
# Copying org_array to copy_array
# using Assignment operator
copy_array = org_array
  
# modifying org_array
org_array[1, 2] = 13
  
# checking if copy_array has remained the same
  
# printing original array
print('Original Array: \n', org_array)
  
# printing copied array
print('\nCopied Array: \n', copy_array)

Output:

In the above example, the given Numpy array ‘org_array‘ is copied to another array ‘copy_array‘ using Assignment Operator.




My Personal Notes arrow_drop_up
Recommended Articles
Page :