Is there in numpy a function that does: np.concatenate([a_[np.newaxis] for a_ in a]) ? ie: add a dimension in front and stack along this dimension, just like np.array(a) would do, but more efficient. This is something that do all the time. Am I the only one? Cheers, Gaël
On Tue, Jul 20, 2010 at 5:11 AM, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Is there in numpy a function that does:
np.concatenate([a_[np.newaxis] for a_ in a])
?
ie: add a dimension in front and stack along this dimension, just like
np.array(a)
would do, but more efficient.
This is something that do all the time. Am I the only one?
Will one of the stack functions do? I take it your a looks something like a = [np.arange(1000), np.arange(1000), np.arange(1000)] np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a])) # True It's about the same speed-wise as concatenate, but it's more terse and faster than np.array if you already have a list of arrays. Skipper
On Tue, Jul 20, 2010 at 7:24 AM, Skipper Seabold <jsseabold@gmail.com> wrote:
On Tue, Jul 20, 2010 at 5:11 AM, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Is there in numpy a function that does:
np.concatenate([a_[np.newaxis] for a_ in a])
?
ie: add a dimension in front and stack along this dimension, just like
np.array(a)
would do, but more efficient.
This is something that do all the time. Am I the only one?
Will one of the stack functions do? I take it your a looks something like
a = [np.arange(1000), np.arange(1000), np.arange(1000)]
np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a])) # True
It's about the same speed-wise as concatenate, but it's more terse and faster than np.array if you already have a list of arrays.
Stacking seems to add some overhead. A reshape is needed if the arrays in the list are more than 1d.
a = [np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]])] timeit np.array(a) 100000 loops, best of 3: 6.76 us per loop timeit np.concatenate([a_[np.newaxis] for a_ in a]) 100000 loops, best of 3: 4.17 us per loop timeit np.vstack(a).reshape(2,2,2) 100000 loops, best of 3: 5.91 us per loop
On Tue, Jul 20, 2010 at 10:24:56AM -0400, Skipper Seabold wrote:
Will one of the stack functions do? I take it your a looks something like
a = [np.arange(1000), np.arange(1000), np.arange(1000)]
np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a])) # True
Works only for 1D arrays: In [3]: b = np.arange(100).reshape((10, 10)) In [4]: a = [b, b, b] In [5]: np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a])) Out[5]: False Gaël
On Tue, Jul 20, 2010 at 10:35 AM, Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
On Tue, Jul 20, 2010 at 10:24:56AM -0400, Skipper Seabold wrote:
Will one of the stack functions do? I take it your a looks something like
a = [np.arange(1000), np.arange(1000), np.arange(1000)]
np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a])) # True
Works only for 1D arrays:
In [3]: b = np.arange(100).reshape((10, 10))
In [4]: a = [b, b, b]
In [5]: np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a])) Out[5]: False
Hmm, yeah, wouldn't work without reshaping. np.r_[a] works, but isn't going to win you any speed contests. Skipper
participants (3)
-
Gael Varoquaux
-
Keith Goodman
-
Skipper Seabold