numpy 或 scipy 中的左逆?

发布于 2024-08-21 10:37:04 字数 331 浏览 1 评论 0原文

我正在尝试使用 numpy 或 scipy 获取 python 中非方阵的左逆矩阵。 如何将以下 Matlab 代码转换为 Python?

>> A = [0,1; 0,1; 1,0]

A =

     0     1
     0     1
     1     0

>> y = [2;2;1]

y =

     2
     2
     1

>> A\y

ans =

    1.0000
    2.0000

Matlab 中是否有与左逆 \ 运算符等效的 numpy 或 scipy?

I am trying to obtain the left inverse of a non-square matrix in python using either numpy or scipy.
How can I translate the following Matlab code to Python?

>> A = [0,1; 0,1; 1,0]

A =

     0     1
     0     1
     1     0

>> y = [2;2;1]

y =

     2
     2
     1

>> A\y

ans =

    1.0000
    2.0000

Is there a numpy or scipy equivalent of the left inverse \ operator in Matlab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忱杏 2024-08-28 10:37:05

使用linalg.lstsq(A,y),因为A不是正方形。有关详细信息,请参阅此处。如果 A 是正方形,则可以使用 linalg.solve(A,y),但在您的情况下则不然。

Use linalg.lstsq(A,y) since A is not square. See here for details. You can use linalg.solve(A,y) if A is square, but not in your case.

陪你搞怪i 2024-08-28 10:37:05

lesssq 函数,

from numpy import *
from scipy.sparse import csr_matrix
from scipy.optimize import leastsq
from numpy.random import rand

A=csr_matrix([[0.,1.],[0.,1.],[1.,0.]])
b=array([[2.],[2.],[1.]])

def myfunc(x):
    x.shape = (2,1)
    return (A*x - b)[:,0]

print leastsq(myfunc,rand(2))[0]

这是一种处理稀疏矩阵的方法(根据您的评论,这是您想要的),它使用优化包中生成的

[ 1.  2.]

它有点难看,因为我必须根据什么来匹配形状想要最小平方。也许其他人知道如何使其变得更加整洁。

我还尝试通过使用 LinearOperators 来处理 scipy.sparse.linalg 中的函数,但无济于事。问题是所有这些函数都只能处理平方函数。如果有人找到一种方法可以做到这一点,我也想知道。

Here is a method that will work with sparse matrices (which from your comments is what you want) which uses the leastsq function from the optimize package

from numpy import *
from scipy.sparse import csr_matrix
from scipy.optimize import leastsq
from numpy.random import rand

A=csr_matrix([[0.,1.],[0.,1.],[1.,0.]])
b=array([[2.],[2.],[1.]])

def myfunc(x):
    x.shape = (2,1)
    return (A*x - b)[:,0]

print leastsq(myfunc,rand(2))[0]

generates

[ 1.  2.]

It is kind of ugly because of how I had to get the shapes to match up according to what leastsq wanted. Maybe someone else knows how to make this a little more tidy.

I have also tried to get something to work with the functions in scipy.sparse.linalg by using the LinearOperators, but to no avail. The problem is that all of those functions are made to handle square functions only. If anyone finds a way to do it that way, I would like to know as well.

慈悲佛祖 2024-08-28 10:37:05

对于那些希望解决大型稀疏最小二乘问题的人:

我已将 LSQR 算法添加到 SciPy 中。在下一个版本中,您将能够执行以下操作:

from scipy.sparse import csr_matrix
from scipy.sparse.linalg import lsqr
import numpy as np

A = csr_matrix([[0., 1], [0, 1], [1, 0]])
b = np.array([[2.], [2.], [1.]])

lsqr(A, b)

返回答案 [1, 2]

如果您想在不升级 SciPy 的情况下使用此新功能,您可以从代码存储库下载 lsqr.py,网址为

http://projects.scipy.org/scipy/browser/trunk/scipy/sparse/linalg/isolve/lsqr.py< /a>

For those who wish to solve large sparse least squares problems:

I have added the LSQR algorithm to SciPy. With the next release, you'll be able to do:

from scipy.sparse import csr_matrix
from scipy.sparse.linalg import lsqr
import numpy as np

A = csr_matrix([[0., 1], [0, 1], [1, 0]])
b = np.array([[2.], [2.], [1.]])

lsqr(A, b)

which returns the answer [1, 2].

If you'd like to use this new functionality without upgrading SciPy, you may download lsqr.py from the code repository at

http://projects.scipy.org/scipy/browser/trunk/scipy/sparse/linalg/isolve/lsqr.py

墨落成白 2024-08-28 10:37:05

您还可以查找伪逆函数的等效项 pinvnumpy/scipy 中,作为其他答案的替代方案。

You can also look for the equivalent of the pseudo-inverse function pinv in numpy/scipy, as an alternative to the other answers that is.

碍人泪离人颜 2024-08-28 10:37:05

您可以使用矩阵计算来计算左逆:(

import numpy as np

linv_A = np.linalg.solve(A.T.dot(A), A.T)

为什么?因为:

在此处输入图像描述

)

测试:

np.set_printoptions(suppress=True, precision=3)
np.random.seed(123)

A = np.random.randn(3, 2)
print('A\n', A)

A_linv = np.linalg.solve(A.T.dot(A), A.T)
print('A_linv.dot(A)\n', A_linv.dot(A))

结果:

A
 [[-1.086  0.997]
 [ 0.283 -1.506]
 [-0.579  1.651]]
A_linv.dot(A)
 [[ 1. -0.]
 [ 0.  1.]]

You can calculate the left inverse using matrix calculations:

import numpy as np

linv_A = np.linalg.solve(A.T.dot(A), A.T)

(Why? Because:

enter image description here

)

Test:

np.set_printoptions(suppress=True, precision=3)
np.random.seed(123)

A = np.random.randn(3, 2)
print('A\n', A)

A_linv = np.linalg.solve(A.T.dot(A), A.T)
print('A_linv.dot(A)\n', A_linv.dot(A))

Result:

A
 [[-1.086  0.997]
 [ 0.283 -1.506]
 [-0.579  1.651]]
A_linv.dot(A)
 [[ 1. -0.]
 [ 0.  1.]]
霓裳挽歌倾城醉 2024-08-28 10:37:05

我还没有测试过它,但根据此网页,它是:

linalg.solve(A,y)

I haven't tested it, but according to this web page it is:

linalg.solve(A,y)
清秋悲枫 2024-08-28 10:37:05

您可以使用 scipy.sparse.linalg 中的 lsqr 来求解具有最小二乘法的稀疏矩阵系统

You can use lsqr from scipy.sparse.linalg to solve sparse matrix systems with least squares

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文