小编She*_*ton的帖子

通过 3D x,y,z 散点图数据拟合一条线

我有一些数据点沿着 3d 空间中的一条线聚集。我想要导入 csv 文件中的 x、y、z 数据。我想找到一个方程来表示该线,或垂直于该线的平面,或任何数学上正确的方程。这些数据是相互独立的。也许有比我尝试做的更好的方法,但是......

我试图在这里复制一篇旧帖子,它似乎正在做我想做的事情 Fitting a line in 3D

但似乎过去十年的更新使得代码的第二部分无法工作?或者也许我只是做错了什么。我已经把我从弗兰肯斯坦开始的所有内容都放在了底部。有两行似乎给我带来了问题。

我已经把它们摘录在这里了...

import numpy as np

pts = np.add.accumulate(np.random.random((10,3)))
x,y,z = pts.T

# this will find the slope and x-intercept of a plane
# parallel to the y-axis that best fits the data
A_xz = np.vstack((x, np.ones(len(x)))).T
m_xz, c_xz = np.linalg.lstsq(A_xz, z)[0]

# again for a plane parallel to the x-axis
A_yz = np.vstack((y, np.ones(len(y)))).T
m_yz, c_yz = np.linalg.lstsq(A_yz, z)[0]

# the intersection of those two planes …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib linear-algebra pandas

2
推荐指数
1
解决办法
3198
查看次数

标签 统计

linear-algebra ×1

matplotlib ×1

numpy ×1

pandas ×1

python ×1