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 ifTrue. 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 arraythisarray = np.array([1, 2, 3, 4, 5])# implementing the numpy.copy() functionmyarray = np.copy(thisarray)print(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4: We create an array
thisarraywith5elements using thenumpy.array()function. - Line 7: We copy the content of
thisarrayarray and assign it to another arraymyarray. - Line 9: We print the copied array
myarray.