First page Back Continue Last page Overview Graphics

Views

A slicing operation creates a view on the original array, which is a way of accessing array data. The original array is not copied in memory.

np.may_share_memory() can be used to check if two arrays share

the same memory location.

When a view is modified, the original array changes as well:

>>> import numpy as np

>>> a = np.array([3, 8, 12, 18, 7, 11, 30])

>>> b = a[::2]

>>> c = a[1::2]

>>> np.may_share_memory(b, c)

True

>>> b[0] = 1001

>>> b

array([ 3, 12, 7, 30])

>>> c

array([ 8, 18, 11])

>>> a

array([1001, 8, 12, 18, 7, 11, 30])

The memory-bounds of b and c are computed by may_share_memory. If they overlap then this function returns True. Otherwise, it returns False. A return of True does not necessarily mean that the two arrays share any element. It just means that they *might*. It may give you false positives, but it will not give you false negatives.