I’m attempting to append a 1D array which have generated by appending elements one at a time to a 2D array as a new row in the array.
a = np.ones((2, 5), int) b = np.empty((0, 5), int) b = np.append(b, [1]) b = np.append(b, [2]) b = np.append(b, [3]) b = np.append(b, [4]) b = np.append(b, [5]) a = np.append(a, b, axis=0) print(b)
I’m pretty lost as to why this code doesn’t work? They are both arrays of 5 elements, but get the following error? “ValueError: all the input arrays must have same number of dimensions”
Advertisement
Answer
a and b have different dim
a is (2,5) and b is (5,) reshape b to be (1,5).
Then you can append b
row-wise as following:
result=np.append(a,b.reshape(1,-1),axis=0)