A nit, but it would be nice if 'ones' could fill with a value other than 1.
Maybe an optional val= keyword?
Neal Becker wrote:
A nit, but it would be nice if 'ones' could fill with a value other than 1.
Maybe an optional val= keyword?
What would be the advantage compared to fill ? I would guess ones and zeros are special because those two values are special (they can be defined for many types, as neutral elements for + and *),
David
2009/1/30 David Cournapeau david@ar.media.kyoto-u.ac.jp: Neal Becker wrote:
A nit, but it would be nice if 'ones' could fill with a value other than 1.
Maybe an optional val= keyword?
What would be the advantage compared to fill ? I would guess ones and zeros are special because those two values are special (they can be defined for many types, as neutral elements for + and *),
I couldn't find the numpy fill function, until my tiny brain realized you meant the ndarray method:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.fill.html
Cheers, Scott
On Fri, Jan 30, 2009 at 21:54, Scott Sinclair scott.sinclair.za@gmail.comwrote:
2009/1/30 David Cournapeau david@ar.media.kyoto-u.ac.jp: Neal Becker wrote:
A nit, but it would be nice if 'ones' could fill with a value other than
Maybe an optional val= keyword?
What would be the advantage compared to fill ? I would guess ones and zeros are special because those two values are special (they can be defined for many types, as neutral elements for + and *),
I couldn't find the numpy fill function, until my tiny brain realized you meant the ndarray method:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.fill.html
Cheers, Scott
Is fill function has any advantage over ones(size)*x ?
Is fill function has any advantage over ones(size)*x ?
No intermediate array (inplace) ?
Matthieu
Right now there are 2 options to create an array of constant value:
1) empty (size); fill (val)
2) ones (size) * val
1 has disadvantage of not being an expression, so can't be an arg to a function call. Also probably slower than create+fill @ same time
2 is probably slower than create+fill @ same time
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory. boost::ublas, for example, has this feature.
On Fri, Jan 30, 2009 at 09:22:46AM -0500, Neal Becker wrote:
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory. boost::ublas, for example, has this feature.
What I would actually like is some kind of sparse masked array, which would be a generalization of what you are talking about (your case would be the trivial case of mask=False, and a given fill value).
Gaël
On 1/30/2009 3:22 PM, Neal Becker wrote:
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory.
Which again is a special case of something even cooler: lazy evaluation.
This would require arrays to have immutable buffers. Then an expression like "a*x + y" would result in a symbolic representation of
a * x.buffer + y.buffer
Then the array could evaluate itself (it could even numexpr or a JIT compiler) when needed.
The scheme would be very fragile and complicated if arrays were allowed to have immutable data buffers like those of numpy.
Sturla Molden
On 1/30/2009 3:22 PM, Neal Becker wrote:
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory.
Can't you do that with scary stride tricks? I think I remember some discussion of this a while back.
-Chris
Christopher Barker wrote:
On 1/30/2009 3:22 PM, Neal Becker wrote:
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory.
Can't you do that with scary stride tricks? I think I remember some discussion of this a while back.
I think that's right, but at that point, what gain is that over using a regular constant and relying on numpy's broadcasting?
Ryan
On Fri, Jan 30, 2009 at 11:26, Ryan May rmay31@gmail.com wrote:
Christopher Barker wrote:
On 1/30/2009 3:22 PM, Neal Becker wrote:
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory.
Can't you do that with scary stride tricks? I think I remember some discussion of this a while back.
I think that's right, but at that point, what gain is that over using a regular constant and relying on numpy's broadcasting?
The filled array may be providing some of the shape information that's not in the other arrays.
On Fri, Jan 30, 2009 at 08:22, Neal Becker ndbecker2@gmail.com wrote:
Right now there are 2 options to create an array of constant value:
empty (size); fill (val)
ones (size) * val
1 has disadvantage of not being an expression, so can't be an arg to a function call.
So wrap it in a function.
Also probably slower than create+fill @ same time
Only marginally. In any case, (1) is exactly how ones() and zeros() are implemented. I would be +1 on a patch that adds a filled() function along the lines of ones() and zeros(), but I'm -1 on adding this functionality to ones() or zeros().
2 is probably slower than create+fill @ same time
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory. boost::ublas, for example, has this feature.
In [2]: from numpy.lib.stride_tricks import as_strided
In [3]: def hollow_filled(shape, value, dtype=None): ...: x = asarray(value, dtype=dtype) ...: return as_strided(x, shape, [0]*len(shape)) ...:
In [5]: hollow_filled([2,3,4], 5) Out[5]: array([[[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]],
[[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]]])
In [6]: hollow_filled([2,3,4], 5.0) Out[6]: array([[[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]],
[[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]]])
Robert Kern wrote:
On Fri, Jan 30, 2009 at 08:22, Neal Becker ndbecker2@gmail.com wrote:
Right now there are 2 options to create an array of constant value:
empty (size); fill (val)
ones (size) * val
1 has disadvantage of not being an expression, so can't be an arg to a function call.
So wrap it in a function.
Also probably slower than create+fill @ same time
Only marginally. In any case, (1) is exactly how ones() and zeros() are implemented. I would be +1 on a patch that adds a filled() function along the lines of ones() and zeros(), but I'm -1 on adding this functionality to ones() or zeros().
2 is probably slower than create+fill @ same time
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory. boost::ublas, for example, has this feature.
In [2]: from numpy.lib.stride_tricks import as_strided
In [3]: def hollow_filled(shape, value, dtype=None): ...: x = asarray(value, dtype=dtype) ...: return as_strided(x, shape, [0]*len(shape)) ...:
In [5]: hollow_filled([2,3,4], 5) Out[5]: array([[[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]],
[[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]]])
In [6]: hollow_filled([2,3,4], 5.0) Out[6]: array([[[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]],
[[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]]])
Where can I find doc on stride_tricks? Nothing here: http://docs.scipy.org/doc/numpy/search.html?q=stride_tricks&check_keywor...
On Fri, Jan 30, 2009 at 16:32, Neal Becker ndbecker2@gmail.com wrote:
Robert Kern wrote:
On Fri, Jan 30, 2009 at 08:22, Neal Becker ndbecker2@gmail.com wrote:
Right now there are 2 options to create an array of constant value:
empty (size); fill (val)
ones (size) * val
1 has disadvantage of not being an expression, so can't be an arg to a function call.
So wrap it in a function.
Also probably slower than create+fill @ same time
Only marginally. In any case, (1) is exactly how ones() and zeros() are implemented. I would be +1 on a patch that adds a filled() function along the lines of ones() and zeros(), but I'm -1 on adding this functionality to ones() or zeros().
2 is probably slower than create+fill @ same time
Now what would be _really_ cool is a special array type that would represent a constant array without wasting memory. boost::ublas, for example, has this feature.
In [2]: from numpy.lib.stride_tricks import as_strided
In [3]: def hollow_filled(shape, value, dtype=None): ...: x = asarray(value, dtype=dtype) ...: return as_strided(x, shape, [0]*len(shape)) ...:
In [5]: hollow_filled([2,3,4], 5) Out[5]: array([[[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]],
[[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]]])
In [6]: hollow_filled([2,3,4], 5.0) Out[6]: array([[[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]],
[[ 5., 5., 5., 5.], [ 5., 5., 5., 5.], [ 5., 5., 5., 5.]]])
Where can I find doc on stride_tricks?
Source is always the best place. as_strided is not exposed as such since you can cause segfaults with it if you have a bug. Rather, it's useful for devs to make tools that, once debugged, can't cause segfaults.
Nothing here: http://docs.scipy.org/doc/numpy/search.html?q=stride_tricks&check_keywor...
Use this search box to search the development version of the docs:
http://docs.scipy.org/numpy/search/
On 1/30/2009 3:07 PM, Grissiom wrote:
Is fill function has any advantage over ones(size)*x ?
You avoid filling with ones, all the multiplications and creating an temporary array. It can be done like this:
a = empty(size) a[:] = x
Which would be slightly faster and more memory efficient.
S.M.
On Fri, Jan 30, 2009 at 22:16, Sturla Molden sturla@molden.no wrote:
On 1/30/2009 3:07 PM, Grissiom wrote:
Is fill function has any advantage over ones(size)*x ?
You avoid filling with ones, all the multiplications and creating an temporary array. It can be done like this:
a = empty(size) a[:] = x
Which would be slightly faster and more memory efficient.
Hmm, I +1 on this one. It's more pythonic ;)
On 1/30/2009 2:18 PM, Neal Becker wrote:
A nit, but it would be nice if 'ones' could fill with a value other than 1.
Maybe an optional val= keyword?
I am -1 on this. Ones should fill with ones, zeros should fill with zeros. Anything else is counter-intuitive. Calling numpy.ones to fill with fives makes no sense to me. But I would be +1 on having a function called numpy.values or numpy.fill that would create and fill an ndarray with arbitrary values.
S.M.
Sturla Molden wrote:
On 1/30/2009 2:18 PM, Neal Becker wrote:
A nit, but it would be nice if 'ones' could fill with a value other than 1.
Maybe an optional val= keyword?
I am -1 on this. Ones should fill with ones, zeros should fill with zeros. Anything else is counter-intuitive. Calling numpy.ones to fill with fives makes no sense to me. But I would be +1 on having a function called numpy.values or numpy.fill that would create and fill an ndarray with arbitrary values.
I completely agree here.
Ryan
On Fri, Jan 30, 2009 at 5:18 AM, Neal Becker ndbecker2@gmail.com wrote:
A nit, but it would be nice if 'ones' could fill with a value other than 1.
Maybe an optional val= keyword?
You can use the "tile" function for this. "tile(3,3)" creates an array of 3 3's.
Geoffrey