Original address
The error occurred during the execution of np.concatenate (, axis = 1)
when
When I want to pile up two one-dimensional data, that is
# Pre
a = [1,2,3]
b = [4,5,6]
# New
[[1,2,3],
[4,5,6]]
use np.concatenate ((a,b),axis=1)
This is because both a and B are one-dimensional data with only one dimension, that is, axis = 0, and there is no axis = 1
I found two solutions
np.vstack ((A,B))
A and B can be stacked vertically
print(np.vstack((a,b))) # Note that the parameter passed is '(a,b)'
# [[1 2 3]
# [4 5 6]]
The fly in the ointment is that this method can only pass two vectors to stack
np.newaxis
+ np.concatenate ()
Newaxis, as the name suggests, is a new axis. The usage is as follows
a = a[np.newaxis,:] # Where ':' represents all dimensions (here 3), the shape of a becomes (1, 3), which is now two-dimensional
# [[1 2 3]]
b = b[np.newaxis,:]
# [[4 5 6]]
At this time, I can pile up two (1, 3) vectors into a matrix of (1 * 2, 3) = (2, 3). Note that axis = 0
should be used, that is, the first dimension
print(np.concatenate((a,b),axis=0))
# [[1 2 3]
# [4 5 6]]
relevant
Numpy: matrix merging