In [26]:
# 多行结果输出支持
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

LU 分解

  • 将一个矩阵分解为一个上三角和下三角矩阵的乘积

In [3]:
import numpy as np

In [4]:
def LU(A):
    U = np.copy(A)
    m, n = A.shape
    L = np.eye(n)
    for k in range(n-1):
        for j in range(k+1,n):
            L[j,k] = U[j,k]/U[k,k]
            U[j,k:n] -= L[j,k] * U[k,k:n]
    return L, U

In [15]:
A = np.array([[2,1,1,0],[4,3,3,1],[8,7,9,3],[6,7,9,8]]).astype(np.float)

In [16]:
L, U = LU(A)

In [17]:
L


Out[17]:
array([[1., 0., 0., 0.],
       [2., 1., 0., 0.],
       [4., 3., 1., 0.],
       [3., 4., 1., 1.]])

In [18]:
U


Out[18]:
array([[2., 1., 1., 0.],
       [0., 1., 1., 1.],
       [0., 0., 2., 0.],
       [0., 0., 0., 4.]])

In [19]:
A


Out[19]:
array([[2., 1., 1., 0.],
       [4., 3., 3., 1.],
       [8., 7., 9., 3.],
       [6., 7., 9., 8.]])

In [20]:
L @ U


Out[20]:
array([[2., 1., 1., 0.],
       [4., 3., 3., 1.],
       [8., 7., 9., 3.],
       [6., 7., 9., 8.]])

In [12]:
np.allclose(A, L @ U)


Out[12]:
True

The LU factorization is useful!

Solving Ax = b becomes LUx = b:

  1. find A = LU
  2. solve Ly = b
  3. solve Ux = y

In [27]:
v=np.array([1,2,3])
v
v.shape


Out[27]:
array([1, 2, 3])
Out[27]:
(3,)

In [32]:
v1=np.expand_dims(v, -1)
v1
v1.shape


Out[32]:
array([[1],
       [2],
       [3]])
Out[32]:
(3, 1)

In [39]:
v2 = v[np.newaxis]
v2
v2.shape


Out[39]:
array([[1, 2, 3]])
Out[39]:
(1, 3)

In [40]:
v3 = v[:, np.newaxis]
v3
v3.shape


Out[40]:
array([[1],
       [2],
       [3]])
Out[40]:
(3, 1)

广播运算

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when

  • they are equal, or
  • one of them is 1

稀疏矩阵

There are the most common sparse storage formats:

  • coordinate-wise (scipy calls COO)
  • compressed sparse row (CSR)
  • compressed sparse column (CSC)

In [47]:
import sklearn


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-47-b7c74cbf5af0> in <module>()
----> 1 import sklearn

~/softwares/conda/lib/python3.6/site-packages/sklearn/__init__.py in <module>()
    132 else:
    133     from . import __check_build
--> 134     from .base import clone
    135     __check_build  # avoid flakes unused variable error
    136 

~/softwares/conda/lib/python3.6/site-packages/sklearn/base.py in <module>()
      9 
     10 import numpy as np
---> 11 from scipy import sparse
     12 from .externals import six
     13 from .utils.fixes import signature

~/softwares/conda/lib/python3.6/site-packages/scipy/sparse/__init__.py in <module>()
    227 
    228 from .base import *
--> 229 from .csr import *
    230 from .csc import *
    231 from .lil import *

~/softwares/conda/lib/python3.6/site-packages/scipy/sparse/csr.py in <module>()
     13 from .base import spmatrix
     14 
---> 15 from ._sparsetools import csr_tocsc, csr_tobsr, csr_count_blocks, \
     16         get_csr_submatrix, csr_sample_values
     17 from .sputils import (upcast, isintlike, IndexMixin, issequence,

ImportError: /home/autuanliu/softwares/conda/lib/python3.6/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /home/autuanliu/softwares/conda/lib/python3.6/site-packages/scipy/sparse/_sparsetools.cpython-36m-x86_64-linux-gnu.so)

In [ ]: