I would like to append an array [3, 3, 3] to an array [[1, 1, 1], [2, 2, 2]], so that it becomes [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Here is my code:
import numpy as np
arr1 = np.array([[1, 1, 1],
[2, 2, 2]])
arr2 = np.append(arr1, [3, 3, 3])
print (arr2)
instead of printing [[1, 1, 1], [2, 2, 2], [3, 3, 3]],
it prints [1, 1, 1, 2, 2, 2, 3, 3, 3].
I am quite new to numpy and I do not understand why the 2d array suddenly becomes 1d.
np.appenddocs? It explains the flattening.arr2 = np.append(arr1, [3, 3, 3], axis=0)and it gives me the error:all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)np.appendis basically an alternate way of callingnp.concatenate, as the traceback to your error shows. One array is (2,3) shape, the other (3,). The 2nd should be (1,3) shape to concatenate.vstackis an alternative user ofconcatenatethat takes care of that detail.