What is the numpy.copy() function in Python?

Overview

The numpy.copy() function in Python is used to return a copy of an array of the given object.

Syntax

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

Parameters

The numpy.copy() function takes the following parameters:

  • a: This represents the input data.
  • order: This represents the memory layout of the copy. This is optional.
  • subok: Subclasses will be passed through if True. Otherwise, the returned array will be forced to be a base-class array. This is optional.

Return value

The numpy.copy() function returns a copy of the content of the input data.

Example

import numpy as np
# creating an array
thisarray = np.array([1, 2, 3, 4, 5])
# implementing the numpy.copy() function
myarray = np.copy(thisarray)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array thisarray with 5 elements using the numpy.array() function.
  • Line 7: We copy the content of thisarray array and assign it to another array myarray.
  • Line 9: We print the copied array myarray.

Free Resources