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
Read More:
- ValueError: Found array with dim 4. Estimator expected and ValueError: Expected 2D array, got 1D array i
- For the problem of rejecting old usage errors after numpy is updated, modified in numpy 1.20; for more details and guidance
- Python IndexError: too many indices for array: array is 1-dimensional, but 2 were i..
- [Solved] Pytorch Tensor to numpy error: RuntimeError: Can‘t call numpy() on Tensor that requires grad.报错
- Python error collection: NameError: name ‘numpy’ is not defined
- [Solved] RuntimeError: Numpy is not available (Associated Torch or Tensorflow)
- Python3 Error: Cannot uninstall ‘numpy’. It is a distutils installed project and thus we cannot accurate
- numpy.concatenate() 253rd; 38169Keyerror: 0 [How to Solve]
- Python: RNN principle realized by numpy
- [Solved] matplotlib.units.ConversionError: Failed to convert value(s) to axis units: ‘LiR‘
- Plt.acorr() Function Error: ValueError: object too deep for desired array
- Crawler overtime error socket.timeout: timed out/NameError: name ‘socket‘ is not defined
- Pandas Error: ValueError: setting an array element with a sequence.
- [Solved] theano-GPU Error: pygpu.gpuarray.GpuArrayException: b‘cuMemAlloc: CUDA_ERROR_OUT_OF_MEMORY: out of memory
- How to Solve M1 chip import numpy Error
- [Solved] Original error was: No module named ‘numpy.core._multiarray_umath‘
- [Solved] RuntimeError : PyTorch was compiled without NumPy support
- can‘t multiply sequence by non-int of type ‘numpy.float64‘
- Python ImportError: numpy.core.multiarray failed to import
- [Solved] mnn Import Error: initMNN: init numpy failed