3

I want to append a 2D array created within a for-loop vertically.
I tried append method, but this won't stack vertically (I wan't to avoid reshaping the result later), and I tried the vstack() function, but this won't work on an empty array. Does anyone know how to solve this?

import numpy as np
mat = np.array([])
for i in np.arange(3):
    val = np.random.rand(2, 2)
    mat = np.append(mat,val)

I can think of the following solution:

for i in np.arange(3):
    val = np.random.rand(2, 2)
    if i==0:
        mat = val
    else:
        mat = np.vstack((mat,val))

Is there a solution where I just append the values 'val' without specifying an extra if-else statement?

5
  • 1
    Do you want to append 2D arrays to make a 3D array, or to just expand downwards a 2D array? What is the expected shape of the array? Do you know the shape of the array ahead of time? using np.append is slow because it creates a new array every time it is called. If you can initialize an array with zeros then add the data that would be better. Alternatively, you can create a list of lists, then convert to a numpy array after. Commented May 21, 2021 at 7:55
  • And using vals.append(np.random.rand(2, 2)) inside the loop, then mat = np.vstack(vals) after the loop? In your example case, you could that even with a list comprehension. Commented May 21, 2021 at 8:01
  • I want to have 2D array and append over the rows. Expected is to have shape (6,2) like the second solution. I want to avoid this this if-else statement in the code. Is there a way? Commented May 21, 2021 at 8:12
  • np.append will stack vertically - but you first have to take time to read its docs! np.append is just a crudely written front end to np.concatenate, making you think you are doing something like the list append method. Commented May 21, 2021 at 15:00
  • Why do you want to avoid the if-else in the vstack example? If you look at the [source] for both np.append and np.vstack you'll see that they have if-else` statements, prior to calling np.concatenate. And what's wrong with a reshape after? if and reshape are both cheap compared to the copying required by repeated concatenate. Commented May 21, 2021 at 16:10

2 Answers 2

4

Use np.empty to initialize an empty array and define the axis you want to append across:

import numpy as np
mat = np.empty((0,2))
for i in np.arange(3):
    val = np.random.rand(2, 2)
    mat = np.append(mat,val, axis=0)
print(mat)

Output:

[[0.08527627 0.40567273]
 [0.39701354 0.72642426]
 [0.17540761 0.02579183]
 [0.76271521 0.83032347]
 [0.08105248 0.67986726]
 [0.48079453 0.37454798]]

However, as stated in my comment, if you need to append a lot of times you should look into initializing an array of the correct size then assigning values over using np.append() or appending to a list instead (if you do not know the size of the array) and then creating a numpy array after

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure what happens here: AA = array([[0.8336, 0.7219, 0.4219, 0.5242, 0.2185, 0.5263, 0.9378], [1.5199, 1.3509, 0.8133, 1.0232, 0.4385, 0.7882, 1.7773], [2.1065, 2.185 , 1.352 , 1.5063, 0.7637, 1.4393, 2.7533], [2.9781, 2.749 , 2.1404, 2.426 , 1.1021, 2.5482, 3.6495]]) np.append(np.empty((4,7)),AA,axis=0) becomes then shape (8,7)?
replace np.append(np.empty((4,7)) with np.append(np.empty((0,7)). Empty uses "random" data so contains values (in my case it was the previous values held, so the same values as "AA"). You can also use np.zeros((0,7)) if you do not want this (however it shouldnt matter when the height of the array is 0)
1

Another way to do it is using the * operator. Notice you can do this even if you are not working with Numpy

mat = []
for i in np.arange(3):
    if len(mat) == 0:
        mat = np.random.rand(2, 2)
    else:
        mat = [*mat, *np.random.rand(2, 2)] 
# And in case you need it back as a 2D list and not numpy arrays   
mat = [list(element) for element in mat]

Comments

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.