3

In the following case:

a = np.zeros(35)
# create a view
av = a[3:10]
assert np.may_share_memory(a, av)
some_ind = array([0,5,6,24])
# trying to create a second view of another slice of a 
av2 = a[some_ind] # or a[np.where(some_ind)]
assert np.may_share_memory(a, av2) # raise AssertionError

What would be the procedure such that av2 would be a view of the slice of a corresponding to the indices some_ind? I thought about creating a mask array but found in the doc that copies were made using mask arrays.

In other words, my aim, if it is possible, is to be able to create a view of a slice of an array with indices from the initial array not regularly spaced.

1

1 Answer 1

2

The attributes of an array consist of shape, strides and the data.

a[3:10] is a view because it can use the original data buffer, and just use a different shape (7,) and a different start point in the buffer.

a[some_ind] cannot be a view because [0,5,6,24] is not a regular pattern. It can't be expressed as shape, strides and data pointer. There for is has to have its own data copy.

In [534]: a=np.zeros(25,int)
In [535]: np.info(a)
class:  ndarray
shape:  (25,)
strides:  (4,)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  True
data pointer: 0xafa5cc0
...
In [536]: np.info(a[3:10])
class:  ndarray
shape:  (7,)
strides:  (4,)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  True
data pointer: 0xafa5ccc      # ccc v cc0
....
In [537]: np.info(a[[0,5,6,24]])
class:  ndarray
shape:  (4,)
strides:  (4,)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  True
data pointer: 0xae9c038      # different
...

or looking at the data buffer pointer in decimal format:

In [538]: a.__array_interface__['data']
Out[538]: (184179904, False)
In [539]: a[3:10].__array_interface__['data']
Out[539]: (184179916, False)                   # 12 bytes larger
In [540]: a[[0,5,6]].__array_interface__['data']
Out[540]: (181099224, False)

Another way to put it is: the only alternative to copying elements of a, is to hang on the indexing array (or mask), and apply it each time you need those elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.