What is the numpy.copyto() function in NumPy library with examples?

The numpy.copyto() function is a NumPy library function used to copy the values from one array to another. It takes two arrays as input and copies the values from the source array to the destination array.

Syntax

The syntax for using numpy.copyto() function is as follows:

numpy.copyto(dst, src, where=None)

Parameters

This method takes three argument values as below:

  • dst: This parameter specifies the destination array where the values are copied. It must be a NumPy array of the same shape and data type as the source array. If the dst parameter is not provided, then numpy.copyto() will create a new destination array with the same shape and data type as the source array.
  • src: This parameter specifies the source array from which the values are copied. It must be a NumPy array of the same shape and data type as the destination array.
  • where: An optional parameter that specifies the condition on which the copy operation should be performed. If this parameter is not provided, then the entire array is copied. The where parameter can be a boolean NumPy array of the same shape as the destination array, or it can be a boolean expression that evaluates to a NumPy array of the same shape as the destination array. Only the values in the source array that correspond to True values in the where parameter will be copied to the destination array.

Return value

The numpy.copyto() function does not return anything. It modifies the destination array in place and returns None.

Explanation: numpy.copyto() examples

Let’s discuss some scenarios where copyto() method from numpy library is used to company contents from one array two another with multiple argument values.

Case#1: When where the argument is missing.

import numpy as np

# Create a source array
src = np.array([1, 2, 3, 4, 5])

# Create a destination array
dst = np.zeros(5)

# Copy the values from the source array to the destination array
np.copyto(dst, src)

# Print the destination array
print(dst) # Output: [1. 2. 3. 4. 5.]

In this example, the numpy.copyto() function is called to copy the values from the src array to the dst array. After the copy operation, the dst array is modified to contain the same values as the src array.

Case#2: When a boolean array is passed as where the argument to numpy copyto method.

import numpy as np

# Create a source array
src = np.array([1, 2, 3, 4, 5])

# Create a destination array
dst = np.zeros(5)

# Create a boolean array
mask = np.array([True, False, True, False, True])

# Copy the values from the source array to the destination array where the mask is True
np.copyto(dst, src, where=mask)

print(dst)
# Output: [1. 0. 3. 0. 5.]

In this example, we create a boolean array called mask with the same shape as the dst array. The mask array has True values where we want to copy values from the src array to the dst array. We then call numpy.copyto() with the mask array as the where parameter. The result is that only the values in the src array where the mask is True are copied to the dst array.

Frequently Asked Questions

How does the where parameter work in numpy.copyto()?

The where parameter in numpy.copyto() can either be a NumPy array of booleans with the same shape as the destination array, or a boolean expression that evaluates to such an array. In either case, only the elements in the source array that correspond to True values in the where parameter will be copied to the destination array.

Does numpy.copyto() return anything?

No np.copyto() method in NumPy library does not return anything. Instead, it takes two arrays source array and a destination array.

What is the difference between numpy.copyto() and numpy.ndarray.copy()?

The numpy.copyto() and numpy.ndarray.copy() are both NumPy functions used for copying arrays, but they differ in several ways. numpy.copyto() can copy values between arrays of different shapes and sizes. It also uses a where parameter to copy values based on a boolean mask selectively and modifies the destination array in place without returning anything. On the other hand, numpy.ndarray.copy() only copies values within the same array and returns a new array with a copy of the original array’s data.

Stay in the Loop

Get the weekly email from Algoideas that makes reading the AI/ML stuff instructive. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...