I want to combine several (say 15) long arrays of shape (3072,) into one np.array of shape (15,3072). If have found a solution but that's including a nested if-clause in a for-loop, which seems to be inefficient for me. Is there a more efficient solution to come up with a numpy array of the necessary shape (and not a list)? Here is the code:
# Creating sample array
arr1 = np.array([8, 19, 43], dtype=int)
# What I do at the moment (and it works)
arr_out = np.array([])
for i in range(3):
if i == 0:
arr_out = np.hstack((arr_out, arr1))
else:
arr_out = np.vstack((arr_out, arr1))
arr_out # Output is correct shape (Each "new" array gets a new row.)
array([[ 8., 19., 43.], [ 8., 19., 43.], [ 8., 19., 43.]])
What happens when I use np.append:
# How to do the above without the if loop in the for loop?
arr_out = np.array([])
for i in range(3):
arr_out = np.append(arr_out, arr1, axis=0)
arr_out # Output is not in correct shape
array([ 8., 19., 43., 8., 19., 43., 8., 19., 43.])
Do you see any efficient way of getting to numpy.array shape of the first example without using a list (or at least not having a list in the end)?