[Numpy-discussion] Difference between shape=() and shape=(1,)
Friedrich Romstedt
friedrichromstedt@gmail....
Sat Jul 17 11:58:45 CDT 2010
2010/7/14 John Reid <j.reid@mail.cryst.bbk.ac.uk>:
> That sounds useful but I should have said: sometimes I need to replace
> other values that aren't NaNs.
Sorry, for the double dumb recommendation of nan_to_num, but when
replacing other normal values mabe you can use:
>>> a = numpy.asarray(1)
>>> b = numpy.asarray([10, 1])
>>> a *= (a != 1)
>>> b *= (b != 1)
>>> a
array(0)
>>> b
array([10, 0])
We have tested this for speed and it's even faster than the
slicing-and-insert method.
When having to replace, you can use a composite approach, e.g. to
replace 1 by 10000:
>>> b = (b * (b != 1) + 10000 * (b == 1))
I don't know how to replace nans by values other than zero.
Friedrich
More information about the NumPy-Discussion
mailing list