使用 Excel 和 Python 时矩阵的逆矩阵不同

Inverse of matrix different when using Excel and Python

我有这个矩阵

array([[  4.58437363e-04,   6.87656045e-03,   6.87656045e-02,
      1.10652674e-01,   1.65979011e+00,   1.65979011e+01,
      3.66749891e-03,   5.50124836e-02,   5.50124836e-01,
      8.85221390e-01,   1.32783208e+01,   1.32783208e+02],
   [  6.99837609e-02,   1.39967522e+00,   1.43466710e+01,
      1.46232455e-01,   2.92464911e+00,   2.99776533e+01,
      2.53691133e-01,   5.07382267e+00,   5.20066823e+01,
      5.30092650e-01,   1.06018530e+01,   1.08668993e+02],
   [  2.84535496e-03,   3.41442595e-02,   4.83710343e-01,
      1.84948072e-01,   2.21937687e+00,   3.14411723e+01,
      1.23061602e-02,   1.47673922e-01,   2.09204723e+00,
      7.99900413e-01,   9.59880495e+00,   1.35983070e+02],
   [  3.31593931e-02,   2.32115752e-01,   6.46608166e+00,
      2.42778797e-01,   1.69945158e+00,   4.73418654e+01,
      8.70102475e-02,   6.09071733e-01,   1.69669983e+01,
      6.37051563e-01,   4.45936094e+00,   1.24225055e+02]])

当我使用数组公式

计算Excel中的逆时
=MINVERSE(B27:M38)

我明白了

-1.68568E+17    -1.32003E+15    -8.52171E+14    4.94857E+16 -7.86173E+15    4.96639E+14 -1.13617E+17    2.33346E+15 6.79271E+14 -4.96618E+16    2.85561E+15 4.09202E+13
-3.06571E+16    -5.59088E+14    2.25152E+14 1.31473E+15 3.07834E+14 -5.62038E+13    4.52019E+15 1.70789E+14 -2.28526E+13    4.20304E+15 -2.45416E+14    -1.47953E+12
1.81455E+14 5.59212E+13 -6.6655E+12 2.67828E+14 -3.69694E+13    -1.67831E+12    1.97479E+14 2.98161E+13 -2.22862E+12    5.85569E+13 -7.57931E+12    8.07625E+11
7.85428E+13 -5.42549E+14    2.63919E+13 -2.54175E+16    7.29192E+14 9.38632E+13 1.42654E+16 -3.63692E+13    -6.24915E+13    5.70179E+15 -1.68685E+14    -2.09159E+13
-6.01158E+14    5.7767E+13  -3.66461E+13    1.08182E+15 -1.46389E+14    4.3594E+12  -6.35683E+14    1.32624E+13 9.66142E+12 -9.67009E+14    4.0771E+13  2.74315E+12
-1.03323E+14    -1.55034E+13    2.05109E+12 5.20197E+13 6.17586E+12 -7.19368E+11    6.94553E+13 6.31804E+11 -2.80104E+11    4.07483E+13 -1.84912E+12    -1.18293E+11
5.18468E+16 4.88032E+14 -2.99218E+14    -8.71756E+15    1.28383E+15 1.89111E+14 6.82672E+15 -3.5848E+15 1.508E+14   -2.18052E+16    1.26861E+15 -1.32931E+13
7.49045E+15 2.11314E+14 -2.67962E+13    -3.9188E+14 -5.12058E+13    7.56573E+12 -1.57588E+15    3.06765E+13 -5.03822E+12    -2.60028E+14    1.91038E+13 -1.27174E+11
1.51294E+14 -1.5578E+13 1.14145E+12 -1.22333E+14    1.23247E+13 -3.1036E+11 6.15507E+13 -1.9115E+12 -1.0148E+11 7.22411E+13 -3.48559E+12    -1.47163E+11
2.16878E+16 6.9913E+14  -7.80275E+13    -1.01657E+15    -2.05019E+14    1.06245E+13 -5.32103E+15    6.6262E+13  -1.03726E+13    -1.95913E+15    4.15017E+13 1.10843E+13
-9.77565E+14    -7.57216E+13    4.71499E+12 -1.08066E+14    2.16929E+13 -4.34593E+11    5.51293E+14 -4.82981E+11    -4.12064E+11    1.55974E+14 -4.4425E+12 -7.30823E+11
-2.95506E+13    4.54732E+12 2.37753E+11 1.9272E+13  -3.87232E+11    -75168166104    -3.20143E+13    -5.46016E+11    75013747465 40232740147 7907929619  -2339538070

但是,当我使用 python 使用 numpy

计算逆时
ata_inv=numpy.linalg.inv(ata)

我明白了

array([[ -2.92174491e+17,   8.78092304e+13,  -3.74278555e+14,
      6.85675722e+16,  -7.88519831e+15,   3.97491106e+14,
     -1.19443836e+17,   1.96271342e+15,   7.75339916e+14,
     -4.31451830e+16,   2.66377305e+15,   1.34678664e+13],
   [ -1.20148124e+16,  -1.39577173e+14,   1.62609200e+14,
     -2.05343938e+15,   2.47311333e+14,  -3.70464968e+13,
      2.13473015e+15,   2.02356660e+14,  -3.77977161e+13,
      3.19465019e+15,  -2.20164429e+14,   3.94769138e+12],
   [  1.03452264e+14,   4.49963139e+13,  -5.83232093e+12,
      3.62478360e+14,  -3.57104899e+13,  -2.31688528e+12,
      2.19120097e+14,   3.04727893e+13,  -2.15230839e+12,
      6.27499323e+13,  -8.08233158e+12,   8.14399730e+11],
   [  8.99729769e+15,  -7.33200660e+14,  -2.07714396e+13,
     -2.58353689e+16,   6.90934358e+14,   9.91927528e+13,
      1.47797299e+16,  -3.93583461e+12,  -6.50452782e+13,
      4.86796183e+15,  -1.39660309e+14,  -1.81127868e+13],
   [ -3.48430812e+15,   1.12742047e+12,  -2.70225162e+13,
      1.67561520e+15,  -1.40107200e+14,   1.14260887e+12,
     -3.44439537e+14,   8.36705528e+12,   1.21738153e+13,
     -8.28685468e+14,   3.72852679e+13,   1.98978705e+12],
   [  6.58910302e+13,  -1.22902337e+13,   1.39683509e+12,
      2.27296720e+13,   5.47359894e+12,  -5.34359515e+11,
      5.07774800e+13,   9.33372125e+11,  -3.97227009e+11,
      3.00641665e+13,  -1.53501520e+12,  -6.79505533e+10],
   [  4.59914066e+15,  -9.70909800e+14,  -2.45271675e+14,
     -3.96303832e+15,   1.32677231e+15,   1.79898008e+14,
      1.71089473e+16,  -3.68415009e+15,   2.01159781e+14,
     -2.02750198e+16,   1.29185320e+15,  -2.94060796e+13],
   [  5.29168020e+15,   1.89585534e+14,  -1.64697929e+13,
     -9.16154758e+12,  -4.07498605e+13,   4.84728269e+12,
     -1.45007322e+15,   2.63347736e+13,  -3.81669434e+12,
     -9.78100475e+13,   1.34239556e+13,  -7.53836147e+11],
   [  2.82268043e+14,  -1.59196485e+13,   7.63324745e+11,
     -1.48459210e+14,   1.26222499e+13,  -2.02131815e+11,
      6.36919902e+13,  -1.56430001e+12,  -2.38728387e+11,
      6.81867356e+13,  -3.41941515e+12,  -1.22099795e+11],
   [  1.52015592e+16,   6.32482966e+14,  -4.52320855e+13,
     -5.91644074e+12,  -1.65167701e+14,   2.55304641e+12,
     -4.89017768e+15,   5.33549362e+13,  -7.52567672e+12,
     -1.41538267e+15,   2.19297078e+13,   9.07563720e+12],
   [ -4.64671981e+14,  -7.43091440e+13,   2.03611340e+12,
     -1.63922466e+14,   1.83754071e+13,   9.08311874e+10,
      5.27684329e+14,   7.77828696e+11,  -5.83508093e+11,
      1.08824465e+14,  -2.76885598e+12,  -5.61136635e+11],
   [ -2.89682527e+13,   5.34539735e+12,   2.84749412e+11,
      1.45849833e+13,  -2.84208676e+11,  -6.06452748e+10,
     -3.44726894e+13,  -5.87172107e+11,   5.76165423e+10,
      1.36639289e+12,  -4.78031299e+10,  -4.71743402e+09]])

为什么这些答案不同?哪个是正确的?

最后,我只想求一个多项式的最小二乘估计。我以为我会用逆来做,但我现在认为这不是最好的方法。

您得到不同答案的原因有两个

Fist,存在数值不稳定的问题。看看倒数中的值——它们非常大。这意味着你要求逆的原始矩阵至少有一个非常小的奇异值,这意味着矩阵很难求逆。

其次,您正在尝试反转非方阵。矩阵逆实际上仅在方阵的情况下是唯一的。对于非方阵,正式的矩阵逆不存在。相反,您调用的函数将找到左逆或右逆,但这些通常不是唯一的,因此找到这些逆的两种不同但完全有效的算法实现可能会给出不同的答案。