In practice, we often encounter array splicers, and concatenate is a very useful array manipulation function based on the Numpy library.
1, Concatenate (A1, A2…) Axis =0) official document details
concatenate(...)
concatenate((a1, a2, ...), axis=0)
Join a sequence of arrays along an existing axis.
Parameters
----------
a1, a2, ... : sequence of array_like
The arrays must have the same shape, except in the dimension
corresponding to `axis` (the first, by default).
axis : int, optional
The axis along which the arrays will be joined. Default is 0.
Returns
-------
res : ndarray
The concatenated array.
See Also
--------
ma.concatenate : Concatenate function that preserves input masks.
array_split : Split an array into multiple sub-arrays of equal or
near-equal size.
split : Split array into a list of multiple sub-arrays of equal size.
hsplit : Split array into multiple sub-arrays horizontally (column wise)
vsplit : Split array into multiple sub-arrays vertically (row wise)
dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).
stack : Stack a sequence of arrays along a new axis.
hstack : Stack arrays in sequence horizontally (column wise)
vstack : Stack arrays in sequence vertically (row wise)
dstack : Stack arrays in sequence depth wise (along third dimension)
2. Parameters
The parameter passed in must be a tuple or list of multiple arrays. In addition, the direction of splicing should be specified. The default is Axis = 0, which means longitudinal splicing of array objects on axis 0 (longitudinal splicing along axis= 1). Note: Generally, Axis = 0 means to operate on the array of this axis, and the operation direction is another axis, namely Axis =1.
In [23]: a = np.array([[1, 2], [3, 4]])
In [24]: b = np.array([[5, 6]])
In [25]: np.concatenate((a, b), axis=0)
Out[25]:
array([[1, 2],
[3, 4],
[5, 6]])
The incoming array must have the same shape, and the same shape here is sufficient for the same shape between the arrays on the axis axis in the splicing direction
If the array object is splicing axis= 1, the direction is horizontal axis 0, a is a 2*2 dimensional array, Axis = 0 is 2, B is a 1*2 dimensional array, axis= 0 is 1, the shapes of the two are different, then an error will be reported
In [27]: np.concatenate((a,b),axis = 1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-aa1228decc36> in <module>()
----> 1 np.concatenate((a,b),axis = 1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
Transpose B, and b is a 2*1 dimensional array:
In [28]: np.concatenate((a,b.T),axis = 1)
Out[28]:
array([[1, 2, 5],
[3, 4, 6]])
Read More:
- Detailed explanation of yield in Python — the simplest and clearest explanation
- ValueError: need at least one array to concatenate
- Detailed explanation of UART, SPI and IIC and their differences and relations
- Detailed explanation of Python__ new__() method
- Dataframe and np.array The mutual transformation of
- HTTP error 401 and 403 detailed explanation and solution
- In pandas, dataframe and np.array The mutual transformation between the two
- Python error: permissionerror: [errno 13] detailed explanation of permission denied solution
- Windowserror: [error 183] error and in Python os.raname () detailed explanation
- Mac boot boot the most detailed explanation, so that the automatic start of the program can not escape
- Using np.set in Python 3_ Printoptions (threshold = NP. Nan) raises an error resolution
- Array of PHP_ diff,array_ intersect,array_ merge, in_ Is there a limit on the number of arrays in array?
- numpy.logspace () produces an array
- Tensorflow ValueError: Failed to convert a NumPy array to a Tensor
- NotImplementedError: Cannot convert a symbolic Tensor (LSTM/strided_slice:0) to a numpy array
- Record a problem of no module named ‘tensorflow. Examples’ and’ tensorflow. Examples. Tutorials’ in tensorflow 2.0
- [solved] error: valueerror: expected 2D array, got scalar array instead
- Two dimensional array and pointer to one dimensional array
- Numpy adds a new dimension: newaxis
- Warning when using numpy: runtimewarning: numpy.dtype size changed, may indicate binary incompatibility