NumPy Array manipulation: copyto() function

numpy.copyto() function

The copyto() function is used to copy values from one array to another, broadcasting as necessary.
Raises a TypeError if the casting rule is violated, and if where is provided, it selects which elements to copy.
New in version 1.7.0.

Version: 1.15.0


numpy.copyto(dst, src, casting=’same_kind’, where=True)


Name Description Required /
dst The array into which values are copied. Required
src The array from which values are copied. Required
casting Controls what kind of data casting may occur when copying.
  • 'no' means the data types should not be cast at all.
  • ‘equiv’ means only byte-order changes are allowed.
  • 'safe' means only casts which can preserve values are allowed.
  • same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.
  • 'unsafe' means any data conversions may be done.
where A boolean array which is broadcasted to match the dimensions of dst, and selects elements to copy from src to dst wherever it contains the value True. Optional

Example: numpy.copyto()

def copy_parameters_from(self, params):
        """Copies parameters from another source without reallocation.

            params (Iterable): Iterable of parameter arrays.

        for dst, src in zip(self.parameters, params):
            if isinstance(dst, numpy.ndarray):
                if isinstance(src, numpy.ndarray):
                    numpy.copyto(dst, src)
                    dst[:] = src.get()
            elif isinstance(src, numpy.ndarray):
                cuda.copy(src, out=dst) 

Python - NumPy Code Editor:

Previous: NumPy Array manipulation Home
Next: Changing array shape reshape()