I have a collection of several data of fixed shape, and I need to store these values in an array, only creating a new dimension, so I can preserve its initial shape. Here is a small example,
arr = np.array([0,1,2])
values = np.array([[3,4,5], [6,7,8], [9,10,11]])
n = 3
for i in range(n):
arr = np.append(arr, values[n])
I need the code to output something simmilar to
>> array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])
But i can only find
>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Is there a function that can handle this problem?
>Solution :
This is an easy one. Reshape your arr into a 2d array and add an axis argument to np.append.
import numpy as np
arr = np.array([0,1,2])
values = np.array([[3,4,5], [6,7,8], [9,10,11]])
out = np.append(arr.reshape(1,-1),values,axis=0)
print(out)
Yields:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]