How to init an empty np array and add one-dimensional ones to it?

Question:

I try to create an empty array into which I can add other arrays and get a matrix:

arr = np.array([])
arr = np.append(arr, [1, 2])
arr = np.append(arr, [3, 4])

As a result, I get a one-dimensional array:

array([1., 2., 3., 4.])

Expected result:

array([[1., 2.], [3., 4.]])

I tried to init an array as multidimensional arr = np.array([[]]), and not append but concatenate arrays arr = np.concatenate((arr, [1, 2]), axis=0). Both options did not work, the result was again a one-dimensional array.

So, what’s the best way to fix this and get a two-dimensional array as a result?

Asked By: kinayrp

||

Answers:

Numpy arrays have a fixed size. Therefor appending a value to an array is not possible.

There is a number of ways to achieve your goal. The simplest of them is probably just using the np.array method:

np.array([[1., 2.], [3., 4.]])

You can also create a new array of the desired size containing all zeros using np.zeros() and copy the values into this new array afterwards like this:
1

arr = np.zeros((2, 2))
arr[0, :] = [1, 2]
arr[1, :] = [3, 4]

Or you can stack the row arrays using np.stack:

np.stack([[1, 2], [3, 4]])

Edit

If you do not know the required size of the array at creation time you can create a two dimensional array and use np.append to create a copy of the initial array with the new value appended to it:

arr = np.zeros((0, 2)) # Create an empty array with a row length of 2
arr = np.append(arr, [[1, 2]], axis=0)
arr = np.append(arr, [[3, 4]], axis=0)

The double square brackets are crucial because this makes the array two dimensional.

Answered By: M472

You can create a new array by adding another row using:

arr = np.array([])
arr = np.append(arr, [1, 2])
arr = np.row_stack((arr, [3, 4]))
Answered By: user19077881

You need to start with an array with the right dimensions and append using the same dimensions. You also need to note whether you are appending rows or columns, otherwise np.append will flatten the result into a 1d array. Since you are starting with an empty array, you’ll need to use a function that lets you specify the shape on creation. And then wrap the new rows in an outer list to make them 2d.

>>> arr=np.zeros((0,2), dtype=int)
>>> arr = np.append(arr, [[1,2]], axis=0)
>>> arr = np.append(arr, [[3,4]], axis=0)
>>> arr
array([[1, 2],
       [3, 4]])

As a side note, each append copies the array, which gets more and more expensive as the array grows. If you are using append to create a large array row by row, you should consider alternatives.

Answered By: tdelaney

Collect the lists in a list, and do one array construction at the end:

In [687]: alist = []
     ...: alist.append([1,2])
     ...: alist.append([3,4])
     ...: arr = np.array(alist)    
In [688]: alist
Out[688]: [[1, 2], [3, 4]]    
In [689]: arr
Out[689]: 
array([[1, 2],
       [3, 4]])

Repeated np.append and np.concatenate is inefficient, and hard to get right.

Why are you surprised that you got a 1d array. Did you skip the np.append docs? Without axis that’s what is does. With axis it just does np.concatenate. And as you found concatenate is picky about dimensions. You can join a (0,2) with a (1,2) on axis 0, but not a (1,0) with a (2,).

Imitating list methods with arrays doesn’t work – unless you the read documentation carefully.

"empty array" is an ambiguous phrase. np.empty((1000,20,30)) makes a very large array of "arbitrary" values. np.array([]) makes a float dtype array with shape (0,). That has 0 elements, but so does an array with shape (10,0,1000). Lists are inherently 1d; arrays have both shape and dtype.

Answered By: hpaulj
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.