Hello, In order to repeat rows or columns of an array as http://stackoverflow.com/questions/1550130/cloning-row-or-column-vectors I can use np.repeat as suggested by pv. However, looking at the flags of the resulting array, data seems to be copied and actually repeated in memory. This is not applicable if want a 1000x repetition. What are the other options for such a repeat ? On scipy lectures, there is a suggestion to use as_strided : http://scipy-lectures.github.io/advanced/advanced_numpy/#example-fake-dimens... Otherwise, I see broadcast_arrays :
N = 3 data = np.arange(N) np.broadcast_arrays(data[:,None], np.zeros((1,2)))[0] array([[0, 0], [1, 1], [2, 2]])
This works but it feels like invoking a magic formula. Did I miss a simpler function ? best, Pierre
Hey, On Thu, 2013-12-12 at 15:20 +0100, Pierre Haessig wrote:
Hello,
In order to repeat rows or columns of an array as http://stackoverflow.com/questions/1550130/cloning-row-or-column-vectors I can use np.repeat as suggested by pv. However, looking at the flags of the resulting array, data seems to be copied and actually repeated in memory. This is not applicable if want a 1000x repetition.
What are the other options for such a repeat ?
No, I don't think there are any other options. stride tricks are a bit hidden, since in many cases it is more dangerous than helping. Though with some care you can easily implement such functions using stride_tricks. Regards, Sebastian
On scipy lectures, there is a suggestion to use as_strided : http://scipy-lectures.github.io/advanced/advanced_numpy/#example-fake-dimens...
Otherwise, I see broadcast_arrays :
N = 3 data = np.arange(N) np.broadcast_arrays(data[:,None], np.zeros((1,2)))[0] array([[0, 0], [1, 1], [2, 2]])
This works but it feels like invoking a magic formula. Did I miss a simpler function ?
best, Pierre _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Le 13/12/2013 13:45, Sebastian Berg a écrit :
What are the other options for such a repeat ? No, I don't think there are any other options. stride tricks are a bit hidden, since in many cases it is more dangerous than helping. Though with some care you can easily implement such functions using stride_tricks. Thanks ! Since it's in a buried helper function, I could use stride tricks. But for now I'll stick with broadcast_arrays because I have it working.
Maybe the np.repeat function could be changed to avoid copying (when possible) ? best, Pierre
On Tue, 2013-12-17 at 16:41 +0100, Pierre Haessig wrote:
Le 13/12/2013 13:45, Sebastian Berg a écrit :
What are the other options for such a repeat ? No, I don't think there are any other options. stride tricks are a bit hidden, since in many cases it is more dangerous than helping. Though with some care you can easily implement such functions using stride_tricks. Thanks ! Since it's in a buried helper function, I could use stride tricks. But for now I'll stick with broadcast_arrays because I have it working.
Maybe the np.repeat function could be changed to avoid copying (when possible) ?
I doubt it fits repeat well, since repeat also allows for repetition of single elements. The repeats that could be done like this are a few of the many ways repeat can be used. What may make sense is to add one or two thought out functions for common `as_strided` use cases, which always return valid arrays... - Sebastian
best, Pierre _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (2)
-
Pierre Haessig
-
Sebastian Berg