在 3D 中拟合直线
是否有任何算法可以从一组 3D 数据点返回直线方程?我可以找到大量的资料来给出 2D 数据集中的直线方程,但没有一个是 3D 的。
Are there any algorithms that will return the equation of a straight line from a set of 3D data points? I can find plenty of sources which will give the equation of a line from 2D data sets, but none in 3D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试从其他两个值中预测一个值,那么您应该使用
lstsq
和a
参数作为自变量(加上一列 1 来估计截距) ) 和b
作为因变量。另一方面,如果您只想获得数据的最佳拟合线,即如果将数据投影到其上,则该线将最小化真实点与其投影之间的平方距离,那么您想要什么是第一主成分。
定义它的一种方法是,其方向向量是与最大特征值对应的协方差矩阵的特征向量的线,该线穿过数据的平均值。也就是说,eig(cov(data)) 是一种非常糟糕的计算方法,因为它进行了大量不必要的计算和复制,并且可能不如使用 svd 准确。 >。如下所示:
它看起来像这样:
If you are trying to predict one value from the other two, then you should use
lstsq
with thea
argument as your independent variables (plus a column of 1's to estimate an intercept) andb
as your dependent variable.If, on the other hand, you just want to get the best fitting line to the data, i.e. the line which, if you projected the data onto it, would minimize the squared distance between the real point and its projection, then what you want is the first principal component.
One way to define it is the line whose direction vector is the eigenvector of the covariance matrix corresponding to the largest eigenvalue, that passes through the mean of your data. That said,
eig(cov(data))
is a really bad way to calculate it, since it does a lot of needless computation and copying and is potentially less accurate than usingsvd
. See below:Here's what it looks like:
如果您的数据表现得相当好,那么找到分量距离的最小平方和应该就足够了。然后您可以找到 z 与 x 无关,然后再次与 y 无关的线性回归。
按照文档示例:
如果您想最小化从线(与线正交)到 3 空间中的点(我不确定是否称为线性回归)的实际正交距离。然后我将构建一个计算 RSS 的函数并使用 scipy.optimize 最小化函数来解决它。
If your data is fairly well behaved then it should be sufficient to find the least squares sum of the component distances. Then you can find the linear regression with z independent of x and then again independent of y.
Following the documentation example:
If you want to minimize the actual orthogonal distances from the line (orthogonal to the line) to the points in 3-space (which I'm not sure is even referred to as linear regression). Then I would build a function that computes the RSS and use a scipy.optimize minimization function to solve it.