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.
JavaScript
x
13
13
1
a = np.ones((2, 5), int)
2
b = np.empty((0, 5), int)
3
4
b = np.append(b, [1])
5
b = np.append(b, [2])
6
b = np.append(b, [3])
7
b = np.append(b, [4])
8
b = np.append(b, [5])
9
10
a = np.append(a, b, axis=0)
11
12
print(b)
13
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:
JavaScript
1
2
1
result=np.append(a,b.reshape(1,-1),axis=0)
2