【问题标题】:remove independent row or column of a matrix in python [closed]在python中删除矩阵的独立行或列[关闭]
【发布时间】:2014-03-30 10:17:41
【问题描述】:

我必须解决 Ax=b 。我相信解决确定系统/过度确定方程系统的python库是不同的。 python中是否有一个库函数可以从矩阵中删除依赖的行/列,以便从矩阵的秩中我可以适当地求解方程

【问题讨论】:

  • 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是题外话,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。
  • 使用 numpy ,很适合这种操作
  • 基本上我需要解决Ax=b。但在我的情况下,系统可以确定或过度确定。我发现过度确定的系统可以使用最小二乘法解决,而使用最小范数法确定不足。因此,在求解方程组之前,我需要知道矩阵的秩。因此我的问题是在这个方向上。如果我错了,请大家纠正我
  • 矩阵秩的确定是一个相当不适定的问题。您可以做的最好的事情是计算 SVD 并从奇异值的进展中确定是否有足够的幅度下降可用于确定等级。但最好使用 numpy 提供的伪逆或最小二乘求解器,如 user3277291 的答案。

标签: python matrix linear-algebra


【解决方案1】:

您可以使用 A 的pseudoinverse 矩阵,表示为 A+。当且仅当 AA+b = b 并且所有解决方案都由下式给出时,才存在解决方案 x = A+b + (I - A+A)*u

这可以通过 numpy.linalg 来完成

例子:

>>> A = np.array([[1, 2, 3], [4, 5, 6],[8, 10, 12]])
>>> b = np.array([22., 7., 14.])
>>> Ap = np.linalg.pinv(A)
# Check if a solution exist
>>> np.testing.assert_allclose(A.dot(Ap).dot(b),b,rtol=1e-5, atol=0)
>>> x = Ap.dot(b)
>>> print A.dot(x)
[ 22.,   7.,  14.]

您也可以使用 numpy.linalg.lstsq

例子:

>>> A = np.array([[1, 2, 3], [4, 5, 6],[8, 10, 12]])
>>> b = np.array([22., 7., 14.])
>>> x = np.linalg.lstsq(A,b)[0]
>>> print np.dot(A,x)
[ 22.,   7.,  14.]

【讨论】:

    【解决方案2】:

    您可以使用numpy 进行科学计算。例如:

    In [23]: import numpy as np
    
    In [24]: a=np.arange(16).reshape((4,4))
    
    In [25]: a
    Out[25]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    
    In [26]: np.delete(a, 2, axis=1)  #delete the 3rd column
    Out[26]: 
    array([[ 0,  1,  3],
           [ 4,  5,  7],
           [ 8,  9, 11],
           [12, 13, 15]])
    
    In [27]: np.rank(a) #this returns the number of dimensions of an array, 
                        #not the concept "rank" in linear algebra, 
    Out[27]: 2
    
    In [40]: np.linalg.matrix_rank(a) #this returns matrix rank of array using SVD method
    Out[40]: 2
    

    在 Windows 上,您可以获得unofficial installer here

    另一个帖子:Calculate Matrix Rank using scipy

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 2013-12-20
      • 2018-08-16
      • 2017-07-10
      • 1970-01-01
      相关资源
      最近更新 更多