Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating empty array in Numpy

in Matlab I do this:

>> E = []; >> A = [1 2 3 4 5; 10 20 30 40 50]; >> E = [E ; A]  E =       1     2     3     4     5     10    20    30    40    50 

Now I want the same thing in Numpy but I have problems, look at this:

>>> E = array([],dtype=int) >>> E array([], dtype=int64) >>> A = array([[1,2,3,4,5],[10,20,30,40,50]])  >>> E = vstack((E,A)) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/shape_base.py", line 226, in vstack     return _nx.concatenate(map(atleast_2d,tup),0) ValueError: array dimensions must agree except for d_0 

I have a similar situation when I do this with:

>>> E = concatenate((E,A),axis=0) Traceback (most recent call last):   File "<stdin>", line 1, in <module> ValueError: arrays must have same number of dimensions 

Or:

>>> E = append([E],[A],axis=0) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py", line 3577, in append     return concatenate((arr, values), axis=axis) ValueError: arrays must have same number of dimensions 
like image 675
maxv15 Avatar asked Mar 29 '14 14:03

maxv15


People also ask

How do I append an empty NumPy array?

append() Function. If we have an empty array and want to append new rows to it inside a loop, we can use the numpy. empty() function. Since no data type is assigned to a variable before initialization in Python, we have to specify the data type and structure of the array elements while creating the empty array.

Can you concatenate NumPy arrays?

Joining Arrays Using Stack FunctionsWe can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking. We pass a sequence of arrays that we want to join to the stack() method along with the axis.

How do you add elements to an empty array?

You can create empty list by [] . In order to add new item use append . For add other list use extend .

How do you add to an empty array in Python?

You can add elements to an empty list using the methods append() and insert() : append() adds the element to the end of the list. insert() adds the element at the particular index of the list that you choose.


2 Answers

if you know the number of columns before hand:

>>> xs = np.array([[1,2,3,4,5],[10,20,30,40,50]]) >>> ys = np.array([], dtype=np.int64).reshape(0,5) >>> ys array([], shape=(0, 5), dtype=int64) >>> np.vstack([ys, xs]) array([[  1.,   2.,   3.,   4.,   5.],        [ 10.,  20.,  30.,  40.,  50.]]) 

if not:

>>> ys = np.array([]) >>> ys = np.vstack([ys, xs]) if ys.size else xs array([[ 1,  2,  3,  4,  5],        [10, 20, 30, 40, 50]]) 
like image 96
behzad.nouri Avatar answered Oct 05 '22 00:10

behzad.nouri


If you wanna do this just because you cannot concatenate an array with an initialized empty array in a loop, then just use a conditional statement, e.g.

if (i == 0):     do the first assignment else:      start your contactenate  
like image 35
Ming Avatar answered Oct 04 '22 23:10

Ming