Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to initialize and fill an numpy array?

I want to initialize and fill a numpy array. What is the best way?

This works as I expect:

>>> import numpy as np
>>> np.empty(3)
array([ -1.28822975e-231,  -1.73060252e-077,   2.23946712e-314])

But this doesn't:

>>> np.empty(3).fill(np.nan)
>>> 

Nothing?

>>> type(np.empty(3))
<type 'numpy.ndarray'>

It seems to me that the np.empty() call is returning the correct type of object, so I don't understand why .fill() is not working?

Assigning the result of np.empty() first works fine:

>>> a = np.empty(3)
>>> a.fill(np.nan)
>>> a
array([ nan,  nan,  nan])

Why do I need to assign to a variable in order to use np.fill()? Am I missing a better alternative?

like image 755
tbc Avatar asked Mar 14 '14 19:03

tbc


People also ask

How do I fill an array in NumPy?

full() Function. The numpy. full() function fills an array with a specified shape and data type with a certain value. It takes the shape of the array, the value to fill, and the data type of the array as input parameters and returns an array with the specified shape and data type filled with the specified value.

Is it faster to append to NumPy array or list?

array(a) . List append is faster than array append .

How do I fill an empty NumPy array?

Python fill empty numpy arrayempty(). When you create an array with numpy. empty, You need to specify the same shape argument in the output by using the shape parameter.


3 Answers

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+

like image 158
JoshAdel Avatar answered Sep 24 '22 04:09

JoshAdel


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
like image 29
shx2 Avatar answered Sep 22 '22 04:09

shx2


I find this easy to remember:

numpy.array([numpy.nan]*3)

Out of curiosity, I timed it, and both @JoshAdel's answer and @shx2's answer are far faster than mine with large arrays.

In [34]: %timeit -n10000 numpy.array([numpy.nan]*10000)
10000 loops, best of 3: 273 µs per loop

In [35]: %timeit -n10000 numpy.empty(10000)* numpy.nan
10000 loops, best of 3: 6.5 µs per loop

In [36]: %timeit -n10000 numpy.full(10000, numpy.nan)
10000 loops, best of 3: 5.42 µs per loop
like image 27
ryanjdillon Avatar answered Sep 25 '22 04:09

ryanjdillon