0

This Python 3.8 code snippet:

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
a = np.concatenate((a, b), axis=0)
print(a) 

produces:

[[1 2]
 [3 4]
 [5 6]]

as expected. How do I define an empty array e, such that this code produces the same result:

# define en empty array e here
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
e = np.concatenate((e, a), axis=0)
e = np.concatenate((e, b), axis=0)
print(e) 

The use case here is an algorithm, which starts with an empty array and concatenates additional arrays to it in a loop.

Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • 1
    Why not `e = np.concatenate((a, b), axis=0)`? – Dani Mesejo Nov 12 '21 at 11:14
  • 1
    Does this answer your question? [Concatenating empty array in Numpy](https://stackoverflow.com/questions/22732589/concatenating-empty-array-in-numpy) – Mahrkeenerh Nov 12 '21 at 11:14
  • @DaniMesejo I have to start with an empty array for brevity and elegance. – Paul Jurczak Nov 12 '21 at 11:17
  • 2
    Elegance is in the eye of the beholder... and the answer posted is for sure not more breve (in terms of LOC) that the one in the question. – Dani Mesejo Nov 12 '21 at 11:19
  • @Mahrkeenerh Yes, `np.array([], shape=(0, 5), dtype=int64)` would work too. I answered with a slightly different option. – Paul Jurczak Nov 12 '21 at 11:20
  • 1
    @DaniMesejo Obviously not an elegance of the snippet I posted. Brevity of an algorithm, which assumes an empty array at the beginning. – Paul Jurczak Nov 12 '21 at 11:27
  • @Mahrkeenerh, the answers in that 2014 link are mostly unsatisfactory. While it is possible to create an initial array with an appropriate 0 dimension, it's more efficient to build a list of arrays, and do just one `concatenate` at the end. – hpaulj Nov 12 '21 at 17:07
  • @hpaulj op wanted to do it in this way, I provided an answer that does it this way. – Mahrkeenerh Nov 12 '21 at 18:00

1 Answers1

1

Found it:

e = np.empty((0, 2))
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
e = np.concatenate((e, a), axis=0)
e = np.concatenate((e, b), axis=0)
print(e) 
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • better yet `f = np.concatenate((e,a,b), axis=0)`. One concatenate on the full list is better than repeated ones. Less copying. – hpaulj Nov 12 '21 at 15:49
  • @hpaulj Yes, but that wasn't the question. – Paul Jurczak Nov 14 '21 at 16:34
  • Sorry, I mistook you for a beginner who didn't realize that list append is more efficient than repeated `np.concatenate`. List append works in-place, and just adds a reference. `concatenate` makes a new array each time, with all the copying that entails. So yes, if you want to concatenate a "empty" array it needs to have the same number of dimensions as the others, here `(0,2), (2,2) , (1,2)`. Calling `np.empty((0,2))` "empty" is a bit of a misnomer. It shouldn't be confused with the list `[]`. – hpaulj Nov 14 '21 at 16:56
  • Watch the `dtype` with this code. `e` is a float, `a` is int. Conditional code that sets `e=a` on the first iteration may be safer, and a bit faster (one less concatenate), even it it isn't as pretty. – hpaulj Nov 14 '21 at 18:05