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
: 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
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 *),
2009/1/30 David Cournapeau
: Neal Becker wrote: A nit, but it would be nice if 'ones' could fill with a value other than
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 ? -- Cheers, Grissiom
Is fill function has any advantage over ones(size)*x ?
No intermediate array (inplace) ? Matthieu -- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher
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, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
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 -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma
On Fri, Jan 30, 2009 at 11:26, Ryan May
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. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Fri, Jan 30, 2009 at 08:22, Neal Becker
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.
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 "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Robert Kern wrote:
On Fri, Jan 30, 2009 at 08:22, Neal Becker
wrote: 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.
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_keywords=yes&area=default
On Fri, Jan 30, 2009 at 16:32, Neal Becker
Robert Kern wrote:
On Fri, Jan 30, 2009 at 08:22, Neal Becker
wrote: 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.
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_keywords=yes&area=default
Use this search box to search the development version of the docs: http://docs.scipy.org/numpy/search/ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
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
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 ;) -- Cheers, Grissiom
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 -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma
participants (11)
-
Christopher Barker
-
David Cournapeau
-
Gael Varoquaux
-
Geoffrey Irving
-
Grissiom
-
Matthieu Brucher
-
Neal Becker
-
Robert Kern
-
Ryan May
-
Scott Sinclair
-
Sturla Molden