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.
On Di, 20151215 at 17:49 +1100, Juan NunezIglesias wrote:
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?
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 readonly 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
Thanks!
Juan. _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpydiscussion
On Di, 20151215 at 08:56 +0100, Sebastian Berg wrote:
On Di, 20151215 at 17:49 +1100, Juan NunezIglesias wrote:
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?
Two reasons:
 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 readonly 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 usecase is covered by the new `broadcast_to` function.
 Sebastian
Thanks!
Juan. _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpydiscussion
NumPyDiscussion mailing list NumPyDiscussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpydiscussion
On Tue, Dec 15, 2015 at 8:29 PM, Sebastian Berg sebastian@sipsolutions.net wrote:
Actually, your particular usecase is covered by the new `broadcast_to` function.
So it is! Fascinating, thanks for pointing that out! =)
participants (2)

Juan NunezIglesias

Sebastian Berg