Numpy求解数组

2024-05-29 11:39:06 发布

您现在位置:Python中文网/ 问答频道 /正文

使用numpy.linalg.求解要求解线性代数方程,但接收到的数组的最后两个维度必须是平方误差:

如有任何帮助,我将不胜感激

c = array([[1, 1, 1], [.07, .08, .09]])
d = array([24000, 1870])
z = linalg.solve(c, d)
print(z)

Tags: numpy线性数组array误差printsolve代数方程
1条回答
网友
1楼 · 发布于 2024-05-29 11:39:06

不能将numpy.linalg.solve用于documentationa must be square and of full-rank, i.e., all rows (or, equivalently, columns) must be linearly independent中提到的非方阵。您的矩阵不是正方形的,但是文档中也提到了这一点,if either is not true, use lstsq for the least-squares best “solution” of the system/equation.

下面是一个例子,应该对你有用

c = array([[1, 1, 1], [.07, .08, .09]])
d = array([24000, 1870])
z = linalg.lstsq(c, d)[0]
print(z)

# compare d and c*z to be sure
print(numpy.allclose(d,numpy.dot(c,z))) # should be true

相关问题 更多 >

    热门问题