[Numpy-discussion] Difference between shape=() and shape=(1,)
John Reid
j.reid@mail.cryst.bbk.ac...
Tue Jul 13 11:54:39 CDT 2010
Hi,
I have some arrays of various shapes in which I need to set any NaNs to
0. I have been doing the following:
a[numpy.where(numpy.isnan(a)] = 0.
as you can see here:
In [20]: a=numpy.ones(2)
In [21]: a[1]=numpy.log(-1)
In [22]: a
Out[22]: array([ 1., NaN])
In [23]: a[numpy.where(numpy.isnan(a))]=0.
In [24]: a
Out[24]: array([ 1., 0.])
Unfortunately, I've just discovered that when a.shape == () this doesn't
work at all. For example:
In [41]: a=numpy.array((1.))
In [42]: a.shape
Out[42]: ()
In [43]: a[numpy.where(numpy.isnan(a))]=0.
In [44]: a
Out[44]: array(0.0)
but if the shape is (1,), everything is ok:
In [47]: a=numpy.ones(1)
In [48]: a.shape
Out[48]: (1,)
In [49]: a[numpy.where(numpy.isnan(a))]=0.
In [50]: a
Out[50]: array([ 1.])
What's the difference between the 2 arrays with different shapes?
If I pass a scalar into numpy.asarray() why do I get an array of shape
() back? In my case this has caused a subtle bug.
Is there a better way to set NaNs in an array to 0?
Thanks for any tips,
John.
More information about the NumPy-Discussion
mailing list