Hello - I try to convert an input argument to an array of floats. Input can be an array of integers or floats, or scalar integers or floats. The latter seem to give a problem, as they return an array that doesn't have a length. I don't quite understand what b really is in the example below. Doesn't every array have a length? Is this a bug or a feature? Thanks, Mark
b = asarray(3,'d') size(b)
1
len(b)
Traceback (most recent call last): File "<pyshell#20>", line 1, in ? len(b) TypeError: len() of unsized object
Mark, In your example:
b = asarray(3,'d')
b is really a numpy scalar, so it doesn't have a length. But it does have a size (1) and a ndim (0). If you need to have arrays with a length, you can force the array to have a dimension 1 with atleast_1d(b) or array(b,copy=False,ndmin=1)
On 5/2/07, Pierre GM pgmdevlist@gmail.com wrote:
Mark, In your example:
b = asarray(3,'d')
b is really a numpy scalar, so it doesn't have a length. But it does have a size (1) and a ndim (0). If you need to have arrays with a length, you can force the array to have a dimension 1 with atleast_1d(b) or array(b,copy=False,ndmin=1)
Or just array([1],'d')
Chuck
On Wednesday 02 May 2007 10:00:58 Charles R Harris wrote:
On 5/2/07, Pierre GM pgmdevlist@gmail.com wrote:
Mark,
Or just array([1],'d')
Except that in that case you need to know in advance the input is a scalar to put it in a list. The atleast_1d should work better on any input.
El dc 02 de 05 del 2007 a les 09:52 -0400, en/na Pierre GM va escriure:
Mark, In your example:
b = asarray(3,'d')
b is really a numpy scalar, so it doesn't have a length. But it does have a size (1) and a ndim (0).
Just one correction in terms of the current naming convention: b in this case is a 0-dim array, which is a different beast than a numpy scalar (although they behaves pretty similarly). You can distinguish between them in different ways, but one is using type():
In [24]:type(numpy.asarray(3.)) # a 0-dim array Out[24]:<type 'numpy.ndarray'> In [25]:type(numpy.float64(3.)) # a numpy scalar Out[25]:<type 'numpy.float64'>
Cheers,
On Wednesday 02 May 2007 11:39:29 Francesc Altet wrote:
El dc 02 de 05 del 2007 a les 09:52 -0400, en/na Pierre GM va escriure:
In your example:
b = asarray(3,'d')
b is really a numpy scalar, so it doesn't have a length. But it does have a size (1) and a ndim (0).
Just one correction in terms of the current naming convention: b in this case is a 0-dim array, which is a different beast than a numpy scalar (although they behaves pretty similarly).
Quite true, my bad.
OK, so in my example, I get a zero dimension array. Apparently a feature, not a bug. What I don't understand is why it isn't an array of lenght one? (or: why it isn't a bug?) Is there any use for a zero dimension array? I would very much like it to be a one dimension array. In my application I don' t know whether an integer, float, or array gets passed to the function. This is my syntax: def test(a): b = asarray(a,'d') do something to b.... I thought this makes b an array AND a float. The atleast_1d only makes it an array, not necessarily a float. Any more thoughts on how to do this cleanly? Any reason NOT to have asarray(3,'d') return an array of length 1? Thanks, Mark
On May 2, 5:47 pm, Pierre GM pgmdevl...@gmail.com wrote:
On Wednesday 02 May 2007 11:39:29 Francesc Altet wrote:
El dc 02 de 05 del 2007 a les 09:52 -0400, en/na Pierre GM va escriure:
In your example:
> b = asarray(3,'d')
b is really a numpy scalar, so it doesn't have a length. But it does have a size (1) and a ndim (0).
Just one correction in terms of the current naming convention: b in this case is a 0-dim array, which is a different beast than a numpy scalar (although they behaves pretty similarly).
Quite true, my bad. _______________________________________________ Numpy-discussion mailing list Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion
mark wrote:
OK, so in my example, I get a zero dimension array. Apparently a feature, not a bug. What I don't understand is why it isn't an array of lenght one? (or: why it isn't a bug?)
Because we need a way to get rank-0 arrays.
Is there any use for a zero dimension array?
http://projects.scipy.org/scipy/numpy/wiki/ZeroRankArray
I would very much like it to be a one dimension array. In my application I don' t know whether an integer, float, or array gets passed to the function. This is my syntax: def test(a): b = asarray(a,'d') do something to b.... I thought this makes b an array AND a float. The atleast_1d only makes it an array, not necessarily a float. Any more thoughts on how to do this cleanly?
atleast_1d(a).astype(float)
Put it into a function if you want that to be more concise.
Any reason NOT to have asarray(3,'d') return an array of length 1?
Because we need a way to get a rank-0 array.
On Wednesday 02 May 2007 12:27:10 mark wrote:
Any reason NOT to have asarray(3,'d') return an array of length 1?
Because then, it would be "an array, not necessarily a float" ;) You just noticed yourself that an array of dimension 1 is pretty much like a list, while an array of dimension 0 is pretty much like a scalar. Keeping that in mind, I'm sure that you can see the advantage of 0D arrays: they are indeed arrays AND scalar at the same time...
If you need your inputs to be array or scalar and stay that way, then the "asarray" method is the best. You can just perform some additional test on the dimension of the array, before trying to access its length or its size. A 0D array as a size of 1 and a dimension of zero, and therefore no length. A 1D array as a size of n and a dimension of 1. and a length (n).
Pierre GM wrote:
If you need your inputs to be array or scalar and stay that way
It didn't sound like the OP wanted that. I suspect that what is wanted if for to always be a 1-d array (i.e. vector). To do that, I'd do:
import numpy as N
def test(a):
... b = N.asarray(a, dtype=N.float).reshape((-1,)) ... print b.shape ...
test(5)
(1,)
test((5,))
(1,)
test((5,6,7,8))
(4,)
On Wednesday 02 May 2007 14:45:40 Christopher Barker wrote:
Pierre GM wrote:
If you need your inputs to be array or scalar and stay that way
It didn't sound like the OP wanted that. I suspect that what is wanted if for to always be a 1-d array (i.e. vector). To do that, I'd do:
I beg to differ: your option is equivalent to (and I suspect a bit slower than) atleast_1d, which is what the OP complained about...
Pierre GM wrote:
It didn't sound like the OP wanted that. I suspect that what is wanted if for to always be a 1-d array (i.e. vector). To do that, I'd do:
I beg to differ: your option is equivalent to (and I suspect a bit slower than) atleast_1d, which is what the OP complained about...
There is a difference, but I don't know what the OP wanted.
my method (N.asarray(a, dtype-N.float).reshape((-1,))
will ALWAYS create a 1-d array, even if the original is greater than 1-d
atleast_1d will let an n-d array pass through. It also doesn't look like you can specify a data type with atleast_1d.
I don't think my approach is slower, as it handles the conversion to float, and reshape doesn't copy the data if it doesn't need to. But I doubt speed makes any difference here anyway.
-CHB