You want vstack:
In [45]: a = np.array([[1,2,3]])
In [46]: l = [4,5,6]
In [47]: np.vstack([a,l])
Out[47]:
array([[1, 2, 3],
[4, 5, 6]])
You can stack multiple rows on the condition that The arrays must have the same shape along all but the first axis.
In [53]: np.vstack([a,[[4,5,6], [7,8,9]]])
Out[53]:
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]])
Answer from Padraic Cunningham on Stack OverflowYou want vstack:
In [45]: a = np.array([[1,2,3]])
In [46]: l = [4,5,6]
In [47]: np.vstack([a,l])
Out[47]:
array([[1, 2, 3],
[4, 5, 6]])
You can stack multiple rows on the condition that The arrays must have the same shape along all but the first axis.
In [53]: np.vstack([a,[[4,5,6], [7,8,9]]])
Out[53]:
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]])
Try this:
np.concatenate(([a],[b]),axis=0)
when
a = np.array([1,2,3])
b = np.array([4,5,6])
then result should be:
array([[1, 2, 3], [4, 5, 6]])
python - Numpy append 2D array in for loop over rows - Stack Overflow
do you guys know how to add append an array in a 2d array?
python - Concatenate a NumPy array to another NumPy array - Stack Overflow
python - Want to append 2 2d arrays in numpy - Stack Overflow
Videos
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
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]
say i have array [[1,2,3,4,5]], how can i duplicate it 5 times to [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] ?
In [1]: import numpy as np
In [2]: a = np.array([[1, 2, 3], [4, 5, 6]])
In [3]: b = np.array([[9, 8, 7], [6, 5, 4]])
In [4]: np.concatenate((a, b))
Out[4]:
array([[1, 2, 3],
[4, 5, 6],
[9, 8, 7],
[6, 5, 4]])
or this:
In [1]: a = np.array([1, 2, 3])
In [2]: b = np.array([4, 5, 6])
In [3]: np.vstack((a, b))
Out[3]:
array([[1, 2, 3],
[4, 5, 6]])
Well, the error message says it all: NumPy arrays do not have an append() method. There's a free function numpy.append() however:
numpy.append(M, a)
This will create a new array instead of mutating M in place. Note that using numpy.append() involves copying both arrays. You will get better performing code if you use fixed-sized NumPy arrays.
As the error indicate, the dimensions have to match.
So you could resize a so that it matches the dimension of b and then concatenate (the empty cells are filled with zeros).
a.resize(3,4)
a = a.transpose()
np.concatenate((a,b))
array([[ 1, 0, 0],
[ 2, 0, 0],
[ 3, 0, 0],
[ 4, 0, 0],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
Short answer - no. Numpy arrays need to be 'rectangular'; similar to matrices in linear algebra. You can follow the suggestion here and force it in (at the loss of a lot of functionality) or, if you really need the target, use a data structure like a list which is designed to cope with it.