Np.full (size, 0) vs np.zeros (size) vs. np.empty ()

If you had to choose one of the following three ways to initialize an array with zeros, which would you choose, and why ?

my_arr_1 = np.full(size, 0) 

or

 my_arr_2 = np.zeros(size) 

or

 my_arr_3 = np.empty(size) my_arr_3[:] = 0 
+13
python arrays numpy
source share
4 answers

I would use np.zeros because of its name. I would never use the third idiom, because (1) it takes two statements instead of a single expression and (2) it is harder for NumPy optimizers. In fact, in NumPy 1.10, np.zeros is still the fastest option, despite all the indexing optimizations:

 >>> %timeit np.zeros(1e6) 1000 loops, best of 3: 804 µs per loop >>> %timeit np.full(1e6, 0) 1000 loops, best of 3: 816 µs per loop >>> %timeit a = np.empty(1e6); a[:] = 0 1000 loops, best of 3: 919 µs per loop 

Large array to compare with @John Zwinck's results:

 >>> %timeit np.zeros(1e8) 100000 loops, best of 3: 9.66 µs per loop >>> %timeit np.full(1e8, 0) 1 loops, best of 3: 614 ms per loop >>> %timeit a = np.empty(1e8); a[:] = 0 1 loops, best of 3: 229 ms per loop 
+8
source share

Definitely np.zeros . Not only is this the most idiomatic and common way to do this, it is also the fastest:

 In [1]: size=100000000 In [3]: %timeit np.full(size, 0) 1 loops, best of 3: 344 ms per loop In [4]: %timeit np.zeros(size) 100000 loops, best of 3: 8.75 µs per loop In [5]: %timeit a = np.empty(size); a[:] = 0 1 loops, best of 3: 322 ms per loop 
+5
source share

np.zeros much faster if someone wants to initialize an array with zeros. In case someone just wants to initialize an array of the given form and type, but does not care about the initial entries in the array, np.empty little faster.

See the following key test results:

 >>%timeit np.zeros(1000000) 7.89 µs ± 282 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) >>%timeit np.empty(1000000) 7.84 µs ± 332 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 
+1
source share
 np.zero():always 0 np.empty():Random number, depending on memory condition 

You can see the following at campare

 np.zeros( (3,4) ) array([[ 0., 0., 0., 0.], ... [ 0., 0., 0., 0.], ... [ 0., 0., 0., 0.]]) np.empty((3,4)) array([[1.13224202e+277, 1.73151846e-077, 1.24374310e-047,1.30455491e-076], [3.92384790e+179, 6.01353875e-154, 3.12452337e-033,7.72229932e+140], [1.28654694e-320, 0.00000000e+000, 0.00000000e+000,0.00000000e+000]]) 
0
source share

All Articles