Best way to initialize and fill an numpy array?
np.fill
modifies the array in-place, and returns None
. Therefor, if you're assigning the result to a name, it gets a value of None
.
An alternative is to use an expression which returns nan
, e.g.:
a = np.empty(3) * np.nan
You could also try:
In [79]: np.full(3, np.nan)
Out[79]: array([ nan, nan, nan])
The pertinent doc:
Definition: np.full(shape, fill_value, dtype=None, order='C')
Docstring:
Return a new array of given shape and type, filled with `fill_value`.
Although I think this might be only available in numpy 1.8+