numpy.copy() function can return a new array copy of the given object. In this tutorial, we will use some examples to show you how to use it.
numpy.copy() Vs =
For example:
>>> import numpy as np >>> x = np.array([1, 2, 3]) >>> y = x >>> y array([1, 2, 3])
You can find y is the same to x, same means x and y have the same value and memory address.
>>> id(x) 2788039846608 >>> id(y) 2788039846608
if we change the value in y, x is also be changed.
>>> y[0]=5 >>> y array([5, 2, 3]) >>> x array([5, 2, 3])
Notice: x is not array([1, 2, 3])
However, numpy.copy() only make the value of y is same to x, the memory address is different.
Look at this example:
>>> x = np.array([1, 2, 3]) >>> y = np.copy(x) >>> y array([1, 2, 3]) >>> id(x) 2788038511712 >>> id(y) 2788154247728
The value of y is same to x, but memory address is not.
If we change the value of y, x will not be changed.
>>> y[0]=5 >>> y array([5, 2, 3]) >>> x array([1, 2, 3])
numpy.copy() is same to numpy.ndarray.copy()
For example:
>>> x = np.array([1, 2, 3]) >>> y = x.copy() >>> x array([1, 2, 3]) >>> y array([1, 2, 3]) >>> id(x) 2788039846608 >>> id(y) 2788154247808
From this example, we can find:
np.copy(x) is same to x.copy()