Skip to content Skip to sidebar Skip to footer

Numpy Irregularly Strided Array

Quoting the documentation on numpy array structure in memory: Several algorithms in NumPy work on arbitrarily strided arrays. However, some algorithms require single-segment arr

Solution 1:

For instance:

>>>import numpy as np>>>a = np.arange(10)>>>a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>b = a[::2]>>>b
array([0, 2, 4, 6, 8])

a is a single-segment array, with all the data closely packed next to each other in a single contiguous memory block. b on the other hand is a view into that same memory, with a stride that is twice the element size, skipping over the memory locations of the odd integers.

Being one of those functions that require a single-segment array, if you do np.sort(b), it will first have to copy those chunks to a contiguous block of memory before actually get going with the actual sorting.

Post a Comment for "Numpy Irregularly Strided Array"