12

I'm trying to populate a NumPy array of NumPy arrays. Every time I finish an iteration of a loop I create the array to be added. I would then like to append that array to the end of another array. For example:

first iteration
  np.append([], [1, 2]) => [[1, 2]]
next iteration
  np.append([[1, 2]], [3, 4]) => [[1, 2], [3, 4]]
next iteration
  np.append([[1, 2], [3, 4]], [5, 6]) => [[1, 2], [3, 4], [5, 6]]
etc.

I've tried using np.append but this returns a one dimensional array, i.e.

[1, 2, 3, 4, 5, 6]
3
  • 2
    look up np.vstack although I recommend for efficiency creating a 2d python list first with append as you do, and only at the end call np.array on it if you can. Commented Mar 2, 2017 at 1:18
  • It might not be straightforward using numpy.append. As per docs.scipy.org/doc/numpy/reference/generated/numpy.append.html If you use axis, the array being appended has to be of the same dimension as that of array its being appended to. If you don't use axis, the values being appended will be flattened befor being used. Commented Mar 2, 2017 at 1:31
  • @Julien I can't believe I hadn't thought of that. Besides the efficiency it seems much more straightforward since I'm not actually doing anything with the data that requires it to be in NumPy format until I've calculated the entire two dimensional array. Thanks. Commented Mar 2, 2017 at 1:36

3 Answers 3

8

Nest the arrays so that they have more than one axis, and then specify the axis when using append.

import numpy as np
a = np.array([[1, 2]]) # note the braces
b = np.array([[3, 4]])
c = np.array([[5, 6]])

d = np.append(a, b, axis=0)
print(d)
# [[1 2]
#  [3 4]]

e = np.append(d, c, axis=0)
print(e)
# [[1 2]
#  [3 4]
#  [5 6]]

Alternately, if you stick with lists, use numpy.vstack:

import numpy as np
a = [1, 2]
b = [3, 4]
c = [5, 6]

d = np.vstack([a, b])
print(d)
# [[1 2]
#  [3 4]]

e = np.vstack([d, c])
print(e)
# [[1 2]
#  [3 4]
#  [5 6]]
Sign up to request clarification or add additional context in comments.

2 Comments

For numpy.vstack I guess you meant "stick with lists of arrays"?
To bring it closer to the op's original code, you can also create a numpy array with size 0 in one dimension, and then use append. Something like final_stack = np.zeros((0, 2)); a = [[1, 2]]; final_stack = np.append(final_stack, a, axis=0)
6

I found it handy to use this code with numpy. For example:

loss = None
new_coming_loss = [0, 1, 0, 0, 1]
loss = np.concatenate((loss, [new_coming_loss]), axis=0) if loss is not None else [new_coming_loss]

Practical Use:

self.epoch_losses = None
self.epoch_losses = np.concatenate((self.epoch_losses, [loss.flatten()]), axis=0) if self.epoch_losses is not None else [loss.flatten()]

Copy and paste solution:

def append(list, element):
    return np.concatenate((list, [element]), axis=0) if list is not None else [element]

WARNING: the dimension of list and element should be the same except the first dimension, otherwise you will get:

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

Comments

1

Disclaimer: appending arrays should be the exception, because it is inefficient.

That said, you can achieve your aim by specifying an axis

a = np.empty((0, 2))
a = np.append(a, [[3,6]], axis=0)
a = np.append(a, [[1,4]], axis=0)

1 Comment

if appending is inefficient, which way would you recommend?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.