numpy.copyto() in Python
The numpy.copyto() function copies values from one array to another array.
Syntax
numpy.copyto(dst, src, casting='same_kind', where=True)
Parameter
The numpy. copyto() method consists of three parameters, which are as follows:
dst : It represents the array into which our values are copied.
src : This parameter represents the second array from which the values are copied.
casting : It is an optional parameter which controls the data casting that may occur when copying.
where : It is an optional parameter which takes a boolean array which is broadcasted to match the dimensions of dst.
Return
This function returns a copy of the specified array.
Example 1
#Python Program explaining
#the copyto() function
import numpy as np
# make an array with numpy
arr1 = np.array([1, 2, 3])
arr2 = [4, 5, 6]
print("Array 1: ",arr1)
print("Array 2: ",arr2)
# applying the numpy.copyto() function
np.copyto(arr1, arr2)
print("After copying the array ")
print("Array 1: ",arr1)
print("Array 2: ",arr2)
Output
Array 1: [1 2 3]
Array 2: [4, 5, 6]
After copying the array
Array 1: [4 5 6]
Array 2: [4, 5, 6]
Example 2
#Python Program explaining
#the copyto() function
import numpy as np
# specifying the array
arr1 = np.array([[11, 42, 73], [41, 15, 76]])
arr2 = [[40, 52, 61], [27, 8, 9]]
# applying numpy.copyto() function
np.copyto(arr1, arr2)
print("After copying the array: \n",arr1)
Output
Array: [[40 52 61]
[27 8 9]]