Numpy copy() Function



The Numpy copy() function is used to copy given object into an array. The object can be a tuple, list , nested list, nested tuple and array.

We can create an array using the assignment (=) operator and numpy.copy(). The assignment operator creates a reference to the original array, meaning both variables point to the same memory location. Therefore, if changes are made to one array, they will be reflected in the other.

On the other hand, when we create a NumPy array using numpy.copy(), it creates a new, separate copy of the array. As a result, changes made to one array will not affect the other.

The numpy.array(arr, copy=True) function is similar to the numpy.copy() function, as both generate a new array that duplicates the data from the original, ensuring any modifications to the new array do not affect the original.

Syntax

Following is the syntax of the Numpy copy() function −

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

Parameters

Following are the parameters of the Numpy copy() function −

  • array- The array that to be copied.
  • order(optional)- This specifies the order in which the elements are filled in copied class.
  • subok(optional)- It determines whether to subclass the output array if the data type is changed or to return a base-class array

Return Value

This function returns the array interpretation of given input

Example

Following is an basic example to create a numpy array using Numpy copy() function −

import numpy as np 
# creating an array
my_array=[24,9,1,8,97]
# implementing the numpy.copy() function
copy_Array = np.copy(my_array)
print("Numpy Array:",copy_Array)
print(type(copy_Array))

Output

Following is the output of the above code −

Numpy Array: [24  9  1  8 97]
<class 'numpy.ndarray'>

Example : Copy Numpy Array into Another Array

We can copy numpy array into another array using numpy.copy() function. Here, we have copied my_array into copy_Array using numpy.copy() function −

import numpy as np
# creating an array
my_array=np.array([10,99,56,34,78])
print("Original Array",my_array)
print(type(my_array))
# implementing the numpy.copy() function
copy_Array = np.copy(my_array)
print("Numpy Array:",copy_Array)
print(type(copy_Array))

Output

Following is the output of the above code −

Original Array [10 99 56 34 78]
<class 'numpy.ndarray'>
Numpy Array: [10 99 56 34 78]
<class 'numpy.ndarray'>

Example : Create Array with '=' and numpy.copy()

The assignment operator creates a reference to the same array, while numpy.copy() creates a separate copy, so changes to one array do not affect the other.

In the following example, we can find the difference between creating a numpy array using assignment(=) operator and numpy.copy() function −

import numpy as np
# create an array
my_Array = np.array([10,45,78,15,99]) 
print("Original Numpy Array -", my_Array)
# copy an array using assignment  operator
my_Array1 = my_Array
# copy an array using np.copy()
my_Array2 = np.copy(my_Array)
# change 1st element of new array 
my_Array1[0] = 88
# print both arrays
print("Numpy Array copied using assignment operator -",my_Array1)
print("Numpy Array copied using copy() operator -",my_Array2)

Output

Following is the output of the above code −

Original Numpy Array - [10 45 78 15 99]
Numpy Array copied using assignment operator - [88 45 78 15 99]
Numpy Array copied using copy() operator - [10 45 78 15 99]
numpy_array_creation_routines.htm
Advertisements