numpy.copyto() – The NumPy copyto Python Function

Introduction to numpy.copyto() Function

We can make a duplicate of all the data elements in a NumPy array using the Numpy numpy.copyto() function. The original NumPy array will not be affected if any data elements in the copy are changed.

Syntax:

numpy.copyto(destination, source)

Return: Return a copy of an array

Code Examples of numpy.copyto() Function

Example 01: In this example, we can see how the numpy.copyto() function is use to copy elements from a source array to a destination array.

# import the important module in python
import numpy as np
         
# make an array with numpy
softhunt = np.array([1, 2, 3])
softhunt_array = [7, 3, 7]
         
# applying numpy.copyto() method
np.copyto(softhunt, softhunt_array)
   
print(softhunt)

Output:

[7 3 7]

Code explanation:

  • Line 1: We import the NumPy module.
  • Lines 3–4: We create input arrays, softhunt and softhunt_array, using the array() function.
  • Line 7: We implement the copyto() function by copying the values of array softhunt into array softhunt_array.
  • Line 9: We print the modified array softhunt.

Example 02:

# import the important module in python
import numpy as np
         
# make an array with numpy
softhunt = np.array([[1, 2, 3], [4, 5, 6]])
softhunt_array = [[9, 8, 7], [6, 5, 4]]
         
# applying numpy.copyto() method
np.copyto(softhunt, softhunt_array)
   
print(softhunt)

Output:

[[9 8 7]
 [6 5 4]]

FAQs

What does NumPy () do in Python?

NumPy may be use to conduct a wide range of array-based mathematical operations. It extends Python with powerful data structures that ensure fast computations with arrays and matrices. As well as a large library of high-level mathematical functions that work with these arrays and matrices.

Why do we need NumPy?

NumPy arrays are more compact and quicker than Python lists. An array uses less memory and is easier to work with. NumPy stores data in a significantly less amount of memory and has a way of selecting data types.

What is the use of NumPy in machine learning?

The abbreviation NumPy stands for “Numerical Python”. It is a Python library that may be used to perform a variety of mathematical and scientific activities. It includes multi-dimensional arrays and matrices as well as a number of high-level mathematical functions that work on them. That’s why it is important in machine learning.

Conclusion to numpy.copyto() Function

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. Python List [With Examples]

Leave a Comment