7

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.
    – Julien
    Mar 2 '17 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.
    – opensam
    Mar 2 '17 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.
    – UBears
    Mar 2 '17 at 1:36
4

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
6

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]]
1
  • For numpy.vstack I guess you meant "stick with lists of arrays"? Sep 4 at 8:50
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
  • if appending is inefficient, which way would you recommend?
    – Jürgen K.
    Sep 6 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.