I have the following code:
r = numpy.zeros(shape = (width, height, 9))
It creates a
width x height x 9 matrix filled with zeros. Instead, I"d like to know if there"s a function or way to initialize them instead to
NaNs in an easy way.
You rarely need loops for vector operations in numpy. You can create an uninitialized array and assign to all entries at once:
>>> a = numpy.empty((3,3,)) >>> a[:] = numpy.nan >>> a array([[ NaN, NaN, NaN], [ NaN, NaN, NaN], [ NaN, NaN, NaN]])
I have timed the alternatives
a[:] = numpy.nan here and
a.fill(numpy.nan) as posted by Blaenk:
$ python -mtimeit "import numpy as np; a = np.empty((100,100));" "a.fill(np.nan)" 10000 loops, best of 3: 54.3 usec per loop $ python -mtimeit "import numpy as np; a = np.empty((100,100));" "a[:] = np.nan" 10000 loops, best of 3: 88.8 usec per loop
The timings show a preference for
ndarray.fill(..) as the faster alternative. OTOH, I like numpy"s convenience implementation where you can assign values to whole slices at the time, the code"s intention is very clear.
ndarray.fill performs its operation in-place, so
numpy.empty((3,3,)).fill(numpy.nan) will instead return
Another option is to use
numpy.full, an option available in NumPy 1.8+
a = np.full([height, width, 9], np.nan)
This is pretty flexible and you can fill it with any other number that you want.
I compared the suggested alternatives for speed and found that, for large enough vectors/matrices to fill, all alternatives except
val * ones and
array(n * [val]) are equally fast.
Code to reproduce the plot:
import numpy import perfplot val = 42.0 def fill(n): a = numpy.empty(n) a.fill(val) return a def colon(n): a = numpy.empty(n) a[:] = val return a def full(n): return numpy.full(n, val) def ones_times(n): return val * numpy.ones(n) def list(n): return numpy.array(n * [val]) perfplot.show( setup=lambda n: n, kernels=[fill, colon, full, ones_times, list], n_range=[2 ** k for k in range(20)], xlabel="len(a)", )
For courses in business intelligence or decision support systems. A managerial approach to understanding business intelligence systems. To help future managers use and understand analytics, Business...
The Apache Hadoop software library has come into it’s own. It is the basis for advanced distributed development for a host of companies, government institutions, and scientific research facilities. ...
R for Everyone: Advanced Analytics and Graphics. ...
This book is not just about learning the code; even if you learn to program. If you want to program professionally, learning to code is not enough; For this reason, in addition to helping you program,...