numpy append to empty array code example

Example 1: create an empty numpy array and append

combined_array = np.append(empty_array, to_append)

Example 2: numpy append number to array

import numpy as np
a = np.array([1, 2, 3])
newArray = np.append(a, [10, 11, 12])

Example 3: python numpy how to empty array cycle

In [210]: %%timeit
   .....: l = []
   .....: for i in xrange(1000):
   .....:     l.append([3*i+1,3*i+2,3*i+3])
   .....: l = np.asarray(l)
   .....: 
1000 loops, best of 3: 1.18 ms per loop

In [211]: %%timeit
   .....: a = np.empty((0,3), int)
   .....: for i in xrange(1000):
   .....:     a = np.append(a, 3*i+np.array([[1,2,3]]), 0)
   .....: 
100 loops, best of 3: 18.5 ms per loop

In [214]: np.allclose(a, l)
Out[214]: True

Tags:

Ruby Example