Discussion:
[Numpy-discussion] Why does np.repeat build a full array?
Juan Nunez-Iglesias
2015-12-15 06:49:59 UTC
Permalink
Hi,

I've recently been using the following pattern to create arrays of a
specific repeating value:

from numpy.lib.stride_tricks import as_strided
value = np.ones((1,), dtype=float)
arr = as_strided(value, shape=input_array.shape, strides=(0,))

I can then use arr e.g. to count certain pairs of elements using
sparse.coo_matrix. It occurred to me that numpy might have a similar
function, and found np.repeat. But it seems that repeat actually creates
the full, replicated array, rather than using stride tricks to keep it
small. Is there any reason for this?

Thanks!

Juan.
Sebastian Berg
2015-12-15 07:56:45 UTC
Permalink
Post by Juan Nunez-Iglesias
Hi,
I've recently been using the following pattern to create arrays of a
from numpy.lib.stride_tricks import as_strided
value = np.ones((1,), dtype=float)
arr = as_strided(value, shape=input_array.shape, strides=(0,))
I can then use arr e.g. to count certain pairs of elements using
sparse.coo_matrix. It occurred to me that numpy might have a similar
function, and found np.repeat. But it seems that repeat actually
creates the full, replicated array, rather than using stride tricks to
keep it small. Is there any reason for this?
Two reasons:
1. For most arrays, arrays even the simple repeats cannot be done with
stride tricks. (yours has a dimension size of 1)
2. Stride tricks can be nice, but they can also be
unexpected/inconsistent when you start writing to the result array, so
you should not do it (and the array should preferably be read-only IMO,
as_strided itself does not do that).

But yes, there might be room for a function or so to make some stride
tricks more convenient.

- Sebastian
Post by Juan Nunez-Iglesias
Thanks!
Juan.
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Sebastian Berg
2015-12-15 09:29:07 UTC
Permalink
Post by Sebastian Berg
Post by Juan Nunez-Iglesias
Hi,
I've recently been using the following pattern to create arrays of a
from numpy.lib.stride_tricks import as_strided
value = np.ones((1,), dtype=float)
arr = as_strided(value, shape=input_array.shape, strides=(0,))
I can then use arr e.g. to count certain pairs of elements using
sparse.coo_matrix. It occurred to me that numpy might have a similar
function, and found np.repeat. But it seems that repeat actually
creates the full, replicated array, rather than using stride tricks to
keep it small. Is there any reason for this?
1. For most arrays, arrays even the simple repeats cannot be done with
stride tricks. (yours has a dimension size of 1)
2. Stride tricks can be nice, but they can also be
unexpected/inconsistent when you start writing to the result array, so
you should not do it (and the array should preferably be read-only IMO,
as_strided itself does not do that).
But yes, there might be room for a function or so to make some stride
tricks more convenient.
Actually, your particular use-case is covered by the new `broadcast_to`
function.
Post by Sebastian Berg
- Sebastian
Post by Juan Nunez-Iglesias
Thanks!
Juan.
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Juan Nunez-Iglesias
2015-12-16 05:53:02 UTC
Permalink
Post by Sebastian Berg
Actually, your particular use-case is covered by the new `broadcast_to`
function.
So it is! Fascinating, thanks for pointing that out! =)

Loading...