Error or function: cloning a numpy w / slicing array

Following David Morrissey, answer How to clone a list in python? "I did some performance tests and came up with unexpected behavior when working with w / numpy arrays, I know that a numpy array can / should be cloned w /

clone = numpy.array(original) 

or

 clone = numpy.copy(original) 

but it was incorrectly suggested that slicing would do the trick too. But:

 In [11]: original = numpy.arange(4) In [12]: original Out[12]: array([0, 1, 2, 3]) In [13]: clone = original[:] In [14]: clone Out[14]: array([0, 1, 2, 3]) In [15]: clone[0] = 1 In [16]: clone Out[16]: array([1, 1, 2, 3]) In [17]: original Out[17]: array([1, 1, 2, 3]) 

Is there any good reason for this slight inconsistency, or should I point out a mistake?

+3
source share
1 answer

In numpy, slices are links or “views” in the original array, so they are not copies. This is by design, not a mistake. The reason is that the copy is not as useful as the view.

+11
source

Source: https://habr.com/ru/post/1441675/


All Articles