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 Overflowpython - Numpy append 2D array in for loop over rows - Stack Overflow
python - Concatenate a NumPy array to another NumPy array - Stack Overflow
do you guys know how to add append an array in a 2d array?
python - Want to append 2 2d arrays in numpy - Stack Overflow
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]])
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]])
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]
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.
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]] ?
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.
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]]
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