Quoting the documentation on numpy array structure in memory:
Several algorithms in NumPy work on arbitrarily strided arrays. However, some algorithms require single-segment arrays. When an irregularly strided array is passed in to such algorithms, a copy is automatically made.
What is an irregularly strided array?
Is this one --- numpy.array([[1], [1,2]])
? If it's not, please provide an example of one.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With