I find I often write: np.array ([some list comprehension]) mainly because list comprehensions are just so sweet. But I imagine this isn't particularly efficient. I wonder if numpy has a "better" way, and if not, maybe it would be a nice addition?
2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
I find I often write: np.array ([some list comprehension])
mainly because list comprehensions are just so sweet.
But I imagine this isn't particularly efficient.
Right. Using a generator and np.fromiter() will avoid the creation of the intermediate list. Something like: np.fromiter((i for i in range(x))) # use xrange for Python 2
I wonder if numpy has a "better" way, and if not, maybe it would be a nice addition?
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Francesc Alted
Francesc Alted wrote:
2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
I find I often write: np.array ([some list comprehension])
mainly because list comprehensions are just so sweet.
But I imagine this isn't particularly efficient.
Right. Using a generator and np.fromiter() will avoid the creation of the intermediate list. Something like:
np.fromiter((i for i in range(x))) # use xrange for Python 2
Does this generalize to >1 dimensions?
2016-11-04 14:36 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
Francesc Alted wrote:
2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
I find I often write: np.array ([some list comprehension])
mainly because list comprehensions are just so sweet.
But I imagine this isn't particularly efficient.
Right. Using a generator and np.fromiter() will avoid the creation of the intermediate list. Something like:
np.fromiter((i for i in range(x))) # use xrange for Python 2
Does this generalize to >1 dimensions?
A reshape() is not enough? What do you want to do exactly?
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org https://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Francesc Alted
On Fri, Nov 4, 2016 at 7:12 AM, Francesc Alted <faltet@gmail.com> wrote:
Does this generalize to >1 dimensions?
A reshape() is not enough? What do you want to do exactly?
np.fromiter takes scalar input and only builds a 1D array. So it actually can't combine multiple values at once unless they are flattened out in Python. It could be nice to add support for non-scalar inputs, stacking them similarly to np.array. Likewise, it could be nice to add an axis argument, so it can work similarly to np.stack. More generally, you might want to iterate and rebuild over arbitrary dimension(s) of an array. Something like np.stack([x for x in np.unstack(y, axis)], axis) But, we also don't have an unstack function. This would mostly be syntactic sugar, but I think it would be a nice addition. Such a function actually exists in TensorFlow: https://g3doc.corp.google.com/third_party/tensorflow/g3doc/api_docs/python/a...
On 4 November 2016 at 16:04, Stephan Hoyer <shoyer@gmail.com> wrote:
But, we also don't have an unstack function. This would mostly be syntactic sugar, but I think it would be a nice addition. Such a function actually exists in TensorFlow: https://g3doc.corp.google.com/third_party/tensorflow/g3doc/api_docs/python/a...
That link is behind a login wall. This is the public version: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/api_do...
On Fri, Nov 4, 2016 at 9:04 AM, Stephan Hoyer <shoyer@gmail.com> wrote:
On Fri, Nov 4, 2016 at 7:12 AM, Francesc Alted <faltet@gmail.com> wrote:
Does this generalize to >1 dimensions?
A reshape() is not enough? What do you want to do exactly?
np.fromiter takes scalar input and only builds a 1D array. So it actually can't combine multiple values at once unless they are flattened out in Python. It could be nice to add support for non-scalar inputs, stacking them similarly to np.array. Likewise, it could be nice to add an axis argument, so it can work similarly to np.stack.
itertools.product, itertools.permutation, etc. with np.fromiter (and reshape) is probably also useful here, though it doesn't solve the non-scalar problem. Ryan -- Ryan May
Francesc Alted wrote:
2016-11-04 14:36 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
Francesc Alted wrote:
2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
I find I often write: np.array ([some list comprehension])
mainly because list comprehensions are just so sweet.
But I imagine this isn't particularly efficient.
Right. Using a generator and np.fromiter() will avoid the creation of the intermediate list. Something like:
np.fromiter((i for i in range(x))) # use xrange for Python 2
Does this generalize to >1 dimensions?
A reshape() is not enough? What do you want to do exactly?
I was thinking about: x = np.array ([[L1] L2]) where L1,L2 take the form of a list comprehension, as a means to create a 2-D array (in this example)
On Fri, Nov 4, 2016 at 6:36 AM, Neal Becker <ndbecker2@gmail.com> wrote:
Francesc Alted wrote:
2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2@gmail.com>:
I find I often write: np.array ([some list comprehension])
mainly because list comprehensions are just so sweet.
But I imagine this isn't particularly efficient.
Right. Using a generator and np.fromiter() will avoid the creation of
the
intermediate list. Something like:
np.fromiter((i for i in range(x))) # use xrange for Python 2
Does this generalize to >1 dimensions?
No. -- Robert Kern
participants (6)
-
Daπid
-
Francesc Alted
-
Neal Becker
-
Robert Kern
-
Ryan May
-
Stephan Hoyer