简体   繁体   English

用numpy最小二乘法拟合线性曲面

[英]fitting a linear surface with numpy least squares

So I want to solve the equation z= a + b*y +c*x ,. 所以我想解决方程z= a + b*y +c*x ,. getting a,b,c . 得到a,b,c ie: making a (plane) surface fit to a load of scatter points in 3D space. 即:使(平面)表面适合3D空间中的散射点的负载。

But I can't seem to find anything! 但我似乎找不到任何东西! I thought there would be a simple module for such a simple problem. 我认为这个简单的问题会有一个简单的模块。

I have tried, where x,y,z are arrays; 我试过,其中x,y,z是数组;

ys=zip(x,y)
(coeffs, residuals, rank, sing_vals) = np.linalg.lstsq(ys,z)

am I right in thinking coeffs = b,c? 我是否正确地考虑coeffs = b,c? Or am I going completely in the wrong direction. 或者我完全走错了方向。 I just can't seem to find anything else that will work in 3d... 我似乎无法找到任何可以在3d中工作的东西......

I think you're on the right track. 我认为你走在正确的轨道上。 You could still try following the example of the scipy.linalg documentation , in particular the Solving least-squares...` section 您仍然可以尝试遵循scipy.linalg文档的示例,特别是Solving最小二乘......部分

A = np.column_stack((np.ones(x.size), x, y))
c, resid,rank,sigma = np.linalg.lstsq(A,zi)

(we added a column of 1 for the constant). (我们为常量添加了一列1)。

The constants a, b, and c are the unknowns you need to solve for. 常数a,b和c是您需要解决的未知数。

If you substitute your N (x, y, z) points into the equation, you'll have N equations for 3 unknowns. 如果将N(x,y,z)点替换为等式,则将有3个未知数的N个等式。 You can write that as a matrix: 你可以把它写成矩阵:

[x1 y1 1]{ a }   { z1 }
[x2 y2 1]{ b }   { z2 }
[x3 y3 1]{ c } = { z3 }
    ...
[xn yn 1]        { zn }

Or 要么

Ac = z

where A is an Nx3 matrix, c is a 3x1 vector and z is a 3xN vector. 其中A是Nx3矩阵,c是3x1向量,z是3xN向量。

If you premultiply both sides by the transpose of A, you'll have an equation with a 3x3 matrix that you can solve for the coefficients you want. 如果你通过A的转置预测两侧,你将得到一个3x3矩阵的方程,你可以求解你想要的系数。

Use LU decomposition and forward-back substitution. 使用LU分解和前后替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM