Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 358
Fabricator Avatar asked Mar 18 '23 22:03

Fabricator


1 Answers

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.

like image 75
Jaime Avatar answered Mar 29 '23 14:03

Jaime