This article mainly introduces the NumPy implementation of the integration of multidimensional matrix, List extension method, has a certain reference value, now share to everyone, there is a need for friends can refer to
First, merging multiple numpy matrices
1. Create two multidimensional matrices first
The size of matrix A is (2,3,2)
The size of Matrix B is (3,2,3)
Using the Concatentate function, you can combine two multidimensional matrices
After merging should be (5,3,2)
In [1]: Import numpy as Npin [2]: A = Np.ndarray ((3, 2, 3)) in [3]: b = Np.ndarray ((2, 2, 3)) in [4]: Print (A.shape, b.shape ) (3, 2, 3) (2, 2, 3) in [5]: c = np.concatenate ((A, b), Axis = 0) in [6]: Print (C.shape) (5, 2, 3) in [7]:
Second, the addition of matrices
The addition of matrices is a function of append, and list has this function, but they are used in slightly different ways.
1. Create a Ndarray
2. Then append using the Np.append () function (note that np.append, not a.append)
In [2]: Import NumPy as Npin [3]: a = Np.array ([1, 2, 3, 4, 5]) in [4]: a = Np.append (A, ten) in [5]: Aout[5]: Array ([1, 2, 3, 4, 5,]) in [6]: a = Np.append (A, [1, 2, 3]) in [7]: Aout[7]: Array ([1, 2, 3, 4, 5, 10, 1, 2, 3])
Third, the extension of the list (extend)
1, the extension of the list is to merge two lists
2, using Extend function
In [9]: a = [1, 2, 3, 4]in [ten]: b = [5, 6, 7, 8]in [all]: aout[11]: [1, 2, 3, 4]in []: bout[12]: [5, 6, 7, 8]in []: C = A.extend (b) in [+]: cIn []: aout[15]: [1, 2, 3, 4, 5, 6, 7, 8]
Note that the return value of the Extend function is none, so the output of line 13th C above is empty, and A's value has changed, so it is expanded directly after a, and there is no return value.
Iv. Additions to the list
Append the list directly with append.
1. Create List A
2. Append data to the back of a
in [+]: a = [1, 2,3,4]in []: A.append (6) in [+]: aout[30]: [1, 2, 3, 4, 6]in [31]: