Getting started with numpy

http://blog.csdn.net/Shingle_/article/details/70199680
pydoc sys
pydoc numpy

pydoc -k sklearn


numpy.linalg

1. Matrix and vector product Dot product of
two arrays: numpy.dot(a, b, out= None)
①a and b are both constants or one-dimensional arrays, then return a scalar
[python] view plain copy
In [1]: import numpy as np 
 
In [2]: np.dot(3,4) 
Out[2]: 12 
 
In [3]: np.dot([1,2,3],[4,5,6]) 
Out[3]: 32 
Knowledge points: For a one-dimensional array, the result is equal to the inner product of two vectors: set the vector a=(x1,y1), vector b=(x2,y2), the result is equal to x1*x2+y1*y2
②a and b are two-dimensional arrays, which are equivalent to matrix multiplication
[python] view plain copy
In [5] : a = np.array([[1,2],[3,4],[2,5]]) 
   ...: b = np.array([[2,3,1],[4,5 ,2]]) 
   ...: np.dot(a,b) 
   ...: 
Out[5]: 
array([[10, 13, 5], 
       [22, 29, 11], 
       [24, 31, 12]]) 
Knowledge point: matrix multiplication, the number of rows of the first matrix A must be equal to the columns of the second matrix B Number; the result C obtained by multiplying matrix A by matrix B, the element of the mth row and nth column is equal to the element of the mth row of matrix A times the sum of the corresponding elements of the nth column of matrix B.
③a and b are both N-dimensional, and the result is equal to The sum of the last axis of a and the second to last axis of b
[python] view plain copy
In [3]: import numpy as np 
 
In [4]: ​​a = np.array(range(12)).reshape(2 ,3,1,2) 
   ...: b = np.array(range(12)).reshape(3,2,2) 
   ...: 
 
In [5]: a 
Out[5]: 
array([[ [[ 0, 1]], 
 
        [[ 2, 3]], 
 
        [[ 4, 5]]], 
 
 
       [[[ 6, 7]], 
 
        [[ 8, 9]], 
 
        [[10, 11]]] ]) 
 
In [6]: b 
Out[6]: 
array([[[ 0, 1], 
        [ 2, 3]], 
 
       [[ 4,  5], 
        [ 6,  7]], 
 
       [[ 8,  9], 
        [10, 11]]]) 
 
In [7]: np.dot(a,b) 
Out[7]: 
array([[[[[  2,   3], 
          [  6,   7], 
          [ 10,  11]]], 
 
 
        [[[  6,  11], 
          [ 26,  31], 
          [ 46,  51]]], 
 
 
        [[[ 10,  19], 
          [ 46,  55], 
          [ 82,  91]]]], 
 
 
 
       [[[[ 14,  27], 
          [ 66,  79], 
          [118, 131]]], 
 
 
        [[[ 18,  35], 
          [ 86, 103], 
          [154, 171]]], 
 
 
        [[[ 22,  43], 
          [106, 127], 
          [190, 211]]]]]) 
 
In [8]: np.dot(a,b).shape 
Out[8]: (2, 3, 1, 3, 2) 
Calculation process:
[python] view plain copy
In [9]: np.dot(np.array([[[[0,1]]]]),np.array([[[0,1],[2,3]]])) 
Out[9 ]: array([[[[[2, 3]]]]]) 
 
In [10]: np.dot(np.array([[[[0,1]]]]),np.array([[ [4,5],[6,7]]])) 
Out[10]: array([[[[[6, 7]]]]]) 
 
In [11]: np.dot(np.array([ [[[0,1]]]]),np.array([[[8,9],[10,11]]])) 
Out[11]: array([[[[[10, 11]] ]]]) 
Two-vector dot product: numpy.vdot(a, b)
① Parameters a and b are both high-dimensional arrays. Vdot handles multi-dimensional arrays differently from dot. Instead of performing matrix product, only vector dot product can be performed. Then you need to flatten the array first, and then calculate

[python] view plain copy
In [1]: import numpy as np 
   ...: a = np.array([[1, 4], [5, 6]]) 
   ...: b = np.array([[4, 1], [2, 2]]) 
   ...: np.vdot(a,b) 
   ...: 
Out[1]: 30 
a, b arrays Flattening means converting a multi-dimensional array into a one-dimensional array, you can use the ravel function to process
[python] view plain copy
In [2]: np.vdot(a.ravel(), b.ravel()) 
Out[2]: 30 
②Parameters a, b are complex numbers
[python] view plain copy
In [3]: a = np.array([1+2j,3+4j]) 
   ...: b = np.array([5+6j,7+8j ]) 
   ...: np.vdot(a,b) 
   ...: 
Out[3]: (70-8j) 
 
In [4]: ​​np.vdot(b,a) 
Out[4]: (70+8j )
From the above results, it can be seen that the results calculated by np.vdot(a,b) and np.vdot(b,a) are just conjugate complex relationship with each other (the real part is the same, the imaginary part is opposite to each other), the calculation result To take the complex conjugate of the first parameter in the vdot function and the dot product of the other parameter.
Take the calculation of np.vdot(a, b) as an example:
Step 1: Calculate the conjugate complex number of a c=np.array([1-2j, 3-4j])
Step 2: Calculate the dot product of c and b
[python] view plain copy
In [5]: import numpy as np 
   ...: a = np.array([1+2j,3+4j]) 
   ...: b = np.array([5+6j, 7+8j]) 
   ...: c = np.array([1-2j,3-4j]) 
   ...: d = np.array([5-6j,7-8j]) 
   ...: np .dot(c,b) 
   ...: 
Out[5]: (70-8j) 
 
In [6]: np.dot(a,d) 
Out[6]: (70+8j) 
2. Solve the equation and find Inverse matrix

Inverse matrix: numpy.linalg.inv(a)
[python] view plain copy
In [1]: import numpy as np 
   ...: from numpy.linalg import inv 
   ...: a = np.array([[ 1., 2.], [3., 4.]]) 
   ...: inv(a) 
   ...: 
Out[1]: 
array([[-2. , 1. ], 
       [ 1.5, -0.5 ]]) 
Knowledge point: The dot product of a matrix and its inverse matrix is ​​equal to a unit matrix of the same order. The methods for solving the inverse matrix are: undetermined coefficient method, adjoint matrix method, and elementary transformation method
[python] view plain copy
In [2]: np.dot(a, inv(a)) 
Out[2]: 
array([[ 1.00000000e+00, 1.11022302e-16], 
       [ 0.00000000e+00, 1.00000000e+00]]) 
Compare two arrays: numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
[python] view plain copy
In [3]: np.allclose(np.dot(a,inv(a)),np.eye(2)) 
Out[3]: True 
least square method: numpy.linalg.lstsq(a, b, rcond=-1)
1. b is a one-dimensional array
[python] view plain copy
In [13]: import numpy as np 
    ... : from numpy.linalg import lstsq 
    ...: X1 = np.array([[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0 
    ...: ]]) 
    ...: y1= np.array([[7], [9], [13], [17.5], [18]]) 
    ...: np.linalg.lstsq(X1, y1) 
    ...: 
Out[13]: 
(array([[ 1.1875 ], 
        [ 1.01041667], 
        [ 0.39583333]]), 
array([ 8.22916667]), 
3, 
array([ 26.97402951, 2.46027806, 0.59056212])) 
From the above results: Group, four elements in the tuple, the first element represents the least squares solution sought, the second element represents the sum of the residuals, the third element represents the X1 matrix rank, and the fourth element represents the singular value of X1
2, b is a multidimensional array
[python] view plain copy
In [14]: import numpy as np 
    ...: from numpy.linalg import lstsq 
    ...: X1 = np.array([[1, 6, 2], [1 , 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0 
    ...: ]]) 
    ...: y1= np.array([[7,8 ], [9,7], [13,10], [17.5,16], [18,17]]) 
    ...: np.linalg.lstsq(X1, y1) 
    ...: 
Out[14]: 
(array([[ 1.1875 , -1.125 ], 
        [ 1.01041667, 1.02083333], 
        [ 0.39583333, 1.29166667]]), 
array( [ 8.22916667, 2.91666667]), 
3, 
array([ 26.97402951, 2.46027806, 0.59056212])) 
Through the comparative analysis of the above two results: the dimension of the parameter b increases, the dimensions of the first and second element arrays also change, and the corresponding The K columns represent the least squares solution and the residual sum of the kth column in the b array, respectively.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326463311&siteId=291194637
Recommended