Array shape operations:

You can easily manipulate the shape of an array with some functions.

0 0 0 0 0 0 0 0 0

Both methods are used to modify array shapes, but there are some differences.

  1. The 0 is 0 0 converts the array to the specified shape and returns the result. The shape of the array is 0 0 Call method:

    a1 = np.random.randint(0.10,size=(3.4))
    a2 = a1.reshape((2.6)) # return the modified result without affecting the original array itself
    Copy the code
  2. Resize converts the array to the specified shape, directly modifying the array itself. No value is returned. Call method:

    a1 = np.random.randint(0.10,size=(3.4))
    a1.resize((2.6)) # A1 itself has changed
    Copy the code

Flatten and Ravel methods:

Both methods convert a multidimensional array to a one-dimensional array, but with the following differences:

  1. flattenAfter converting the array to a one-dimensional array, the copy is returned, so subsequent changes to the return value will not affect the previous array.
  2. ravelThe view is returned after the array is converted to a one-dimensional array, so subsequent changes to the return value will affect the previous array. For example:
x = np.array([[1.2], [3.4]])
x.flatten()[1] = 100 The position element of x[0] is still 1
x.ravel()[1] = 100 The position element of x[0] is 100
Copy the code

Data stitching:

If you have multiple arrays that you want to combine, you can use some of these functions to do so.

  1. Vstack: Stacks arrays vertically. The array must have the same number of columns to stack. Example code is as follows:

    a1 = np.random.randint(0.10,size=(3.5))
    a2 = np.random.randint(0.10,size=(1.5))
    a3 = np.vstack([a1,a2])
    Copy the code
  2. Hstack: Stacks arrays horizontally. The rows of the array must be the same to stack. Example code is as follows:

    a1 = np.random.randint(0.10,size=(3.2))
    a2 = np.random.randint(0.10,size=(3.1))
    a3 = np.hstack([a1,a2])
    Copy the code
  3. Concatenate ([],axis) : Superimpose two arrays, horizontally or vertically. If axis=0, it represents superposition in vertical direction (rows), if axis=1, it represents superposition in horizontal direction (columns), and if Axis =None, it combines the two arrays into a one-dimensional array. Notice that if you’re stacking horizontally, the rows have to be the same, and if you’re stacking vertically, the columns have to be the same. Example code is as follows:

    a = np.array([[1.2], [3.4]])
    b = np.array([[5.6]])
    np.concatenate((a, b), axis=0)
    # the results:
    array([[1.2],
        [3.4],
        [5.6]])
    
    np.concatenate((a, b.T), axis=1)
    # the results:
    array([[1.2.5],
        [3.4.6]])
    
    np.concatenate((a, b), axis=None)
    # the results:
    array([1.2.3.4.5.6])
    Copy the code

Array cutting:

An array can be split with hsplit and vsplit and array_split.

  1. Hsplit: Splits horizontally. Used to specify a number of columns to be divided into. You can use numbers to represent sections or arrays to represent places to split. Example code is as follows:

    a1 = np.arange(16.0).reshape(4.4)
    np.hsplit(a1,2) # Split into two parts
    >>> array([[ 0..1.],
         [ 4..5.],
         [ 8..9.],
         [12..13.]]), array([[ 2..3.],
         [ 6..7.],
         [10..11.],
         [14..15.]])]
    
    np.hsplit(a1,[1.2]) # means one cut at subscript 1 and one cut at subscript 2, divided into three parts
    >>> [array([[ 0.],
         [ 4.],
         [ 8.],
         [12.]]), array([[ 1.],
         [ 5.],
         [ 9.],
         [13.]]), array([[ 2..3.],
         [ 6..7.],
         [10..11.],
         [14..15.]])]
    Copy the code
  2. Vsplit: Splits vertically. Used to specify lines to be split into. You can use numbers to represent sections or arrays to represent places to split. Example code is as follows:

    np.vsplit(x,2) # represents a total of two arrays divided by row
    >>> [array([[0..1..2..3.],
         [4..5..6..7.]]), array([[ 8..9..10..11.],
         [12..13..14..15.]])]
    
    np.vsplit(x,(1.2)) # represents dividing by row, subscript 1 and subscript 2
    >>> [array([[0..1..2..3.]]),
        array([[4..5..6..7.]]),
        array([[ 8..9..10..11.],
               [12..13..14..15.]])]
    Copy the code
  3. Split /array_split(array, indicate_OR_seciont,axis) : Used to specify the split mode. During the split, you need to specify whether to split by row or column. Axis =1 indicates that the split is by column, and Axis =0 indicates that the split is by row. Example code is as follows:

    np.array_split(x,2,axis=0) Cut vertically into 2 parts
    >>> [array([[0..1..2..3.],
         [4..5..6..7.]]), array([[ 8..9..10..11.],
         [12..13..14..15.]])]
    Copy the code

Array (matrix) transpose and axis transpose:

An array in NUMpy is really just a matrix in linear algebra. A matrix can be transposed. Ndarray has a T property that returns the transpose of this array. Example code is as follows:

a1 = np.arange(0.24).reshape((4.6))
a2 = a1.T
print(a2)
Copy the code

Another method called transpose returns a View that does not affect the original array. Example code is as follows:

a1 = np.arange(0.24).reshape((4.6))
a2 = a1.transpose()
Copy the code

Now, why do we do matrix transpose, sometimes when we do some calculations. Like when you take the inner product of matrices. I have to take the transpose of the matrix and multiply it by the previous matrix:

a1 = np.arange(0.24).reshape((4.6))
a2 = a1.T
print(a1.dot(a2))
Copy the code