How to add a new line to an empty numpy array

Using standard Python arrays, I can do the following:

arr = [] arr.append([1,2,3]) arr.append([4,5,6]) # arr is now [[1,2,3],[4,5,6]] 

However, I cannot do the same in numpy. For example:

 arr = np.array([]) arr = np.append(arr, np.array([1,2,3])) arr = np.append(arr, np.array([4,5,6])) # arr is now [1,2,3,4,5,6] 

I also looked at vstack , but when I use vstack in an empty array, I get:

 ValueError: all the input array dimensions except for the concatenation axis must match exactly 

So how do I add a new line to an empty array in numpy?

+117
python numpy scipy
Mar 13 '14 at 22:39
source share
6 answers

The way to β€œstart” the array you want is:

 arr = np.empty((0,3), int) 

This is an empty array, but it has the correct dimension.

 >>> arr array([], shape=(0, 3), dtype=int64) 

Then be sure to add the 0 axis:

 arr = np.append(arr, np.array([[1,2,3]]), axis=0) arr = np.append(arr, np.array([[4,5,6]]), axis=0) 

But, @jonrsharpe is right. In fact, if you are going to add to the loop, it would be much faster to add to the list, as in the first example, and then convert to a numpy array at the end, since you really do not use numpy as intended during the loop:

 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 

The multilingual way to do this depends on your application, but it will be more like:

 In [220]: timeit n = np.arange(1,3001).reshape(1000,3) 100000 loops, best of 3: 5.93 Β΅s per loop In [221]: np.allclose(a, n) Out[221]: True 
+166
Mar 14 '14 at 1:03
source share

In this case, you can use the np.hstack and np.vstack functions

 arr = np.array([]) arr = np.hstack((arr, np.array([1,2,3]))) # arr is now [1,2,3] arr = np.vstack((arr, np.array([4,5,6]))) # arr is now [[1,2,3],[4,5,6]] 

You can also use the np.concatenate function.

Greetings

+23
Mar 13 '14 at 22:56
source share

Here is my solution:

 arr = [] arr.append([1,2,3]) arr.append([4,5,6]) np_arr = np.array(arr) 
+19
May 6 '17 at 10:55
source share

using a custom dtype definition, what worked for me was:

 import numpy # define custom dtype type1 = numpy.dtype([('freq', numpy.float64, 1), ('amplitude', numpy.float64, 1)]) # declare empty array, zero rows but one column arr = numpy.empty([0,1],dtype=type1) # store row data, maybe inside a loop row = numpy.array([(0.0001, 0.002)], dtype=type1) # append row to the main array arr = numpy.row_stack((arr, row)) # print values stored in the row 0 print float(arr[0]['freq']) print float(arr[0]['amplitude']) 
+1
Aug 05 '14 at 4:13
source share

If you add new rows for an array in a loop, assign the array directly for the first time in the loop instead of initializing an empty array.

 for i in range(0,len(0,100)): SOMECALCULATEDARRAY = ....... if(i==0): finalArrayCollection = SOMECALCULATEDARRAY else: finalArrayCollection = np.vstack(finalArrayCollection,SOMECALCULATEDARRAY) 

This is mostly useful when the shape of the array is unknown.

+1
Oct 23 '18 at 11:16
source share

I want to make a for loop, but it does not work with the askewchan method, so I changed it.

 x=np.empty((0,3)) y=np.array([1 2 3]) for i in ... x = vstack((x,y)) 
0
Jul 24 '19 at 15:09
source share



All Articles