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]])
Answer from endolith on Stack OverflowVideos
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.
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
You can use hstack and vstack to concatenate arrays:
>>> from numpy import array, hstack, vstack
>>> a = array([1, 2, 3])
>>> b = array([4, 5, 6])
>>> hstack([a, b])
array([1, 2, 3, 4, 5, 6])
>>> vstack([a, b])
array([[1, 2, 3],
[4, 5, 6]])
numpy.append() returns a new array containing the data from its inputs together. It does not modify the inputs themselves, and there would be no way for it to do so. This is because arrays in NumPy are generally not resizable.
Try changing your code to capture the value returned from append(), which will be the array you want.
append() creates a new array which can be the old array with the appended element.
I think it's more normal to use the proper method for adding an element:
a = numpy.append(a, a[0])
When appending only once or once every now and again, using np.append on your array should be fine. The drawback of this approach is that memory is allocated for a completely new array every time it is called. When growing an array for a significant amount of samples it would be better to either pre-allocate the array (if the total size is known) or to append to a list and convert to an array afterward.
Using np.append:
b = np.array([0])
for k in range(int(10e4)):
b = np.append(b, k)
1.2 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Using python list converting to array afterward:
d = [0]
for k in range(int(10e4)):
d.append(k)
f = np.array(d)
13.5 ms ± 277 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Pre-allocating numpy array:
e = np.zeros((n,))
for k in range(n):
e[k] = k
9.92 ms ± 752 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
When the final size is unkown pre-allocating is difficult, I tried pre-allocating in chunks of 50 but it did not come close to using a list.
85.1 ms ± 561 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)