
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, 2015-12-15 at 17:49 +1100, Juan Nunez-Iglesias wrote:
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

On Di, 2015-12-15 at 17:49 +1100, Juan Nunez-Iglesias wrote:
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
participants (2)
-
Juan Nunez-Iglesias
-
Sebastian Berg