You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Views and copies are important concepts for the optimization of your numerical
281
+
computations. Even if we've already manipulated them in the previous section,
282
+
the whole story is a bit more complex.
283
+
284
+
Direct and indirect access
285
+
++++++++++++++++++++++++++
286
+
287
+
First, we have to distinguish between `indexing
288
+
<https://docs.scipy.org/doc/numpy/user/basics.indexing.html#>`_ and `fancy
289
+
indexing <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing>`_. The first will always return a view while the second will return a
290
+
copy. This difference is important because in the first case, modifying the view
291
+
modifies the base array while this isnot true in the second case:
292
+
293
+
.. code:: pycon
294
+
295
+
>>>Z= np.zeros(9)
296
+
>>>Z_view= Z[:3]
297
+
>>> Z_view[...] =1
298
+
>>>print(Z)
299
+
[ 1. 1. 1. 0. 0. 0. 0. 0. 0.]
300
+
>>>Z= np.zeros(9)
301
+
>>>Z_copy= Z[[0,1,2]]
302
+
>>> Z_copy[...] =1
303
+
>>>print(Z)
304
+
[ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
305
+
306
+
Thus, if you need fancy indexing, it's better to keep a copy of you fancy index
0 commit comments