python - 使用归一化系数进行平面拟合

标签 python numpy scipy least-squares

我正在尝试使用 scipy.optimize.leastsq 将 3d 点拟合到 2.5d/3d 平面。

我正在尝试最小化函数:ax + by + c - z

当我向生成的平面添加噪声时,我开始得到 (a,b,c) 的不同结果,而 a 和 b 之间的线性关系仍然正确。

我的问题:有没有办法限制拟合参数的归一化?

我可以在优化后进行标准化,并再次搜索最后一个参数,但感觉效率低下,并且会导致参数 c 发生很大变化,有什么建议吗?

谢谢!

以下是我在同一架飞机上得到的一些不同结果:

[ 12.88343415     6.7993803   4001.717 ] 

[ 14.52913549     7.44262692  3201.1523]

[ 4.37394650e+00   2.20546734e+00   9.56e+03]

[ 24.32259278    12.32581015 -2748.026]

通过 sqrt(a2 +b2 +1) 标准化后:

[-0.97401694  0.20292819 -6.16468053] 
[-0.97527745  0.1976869  -2.46058884] 
[-0.97573799  0.19342358  5.42282738] 
[ -0.97894621   0.17992336  13.52561491] 
[ -0.97821728   0.17834579  24.5345626 ] 

代码:

def least_squares(neighborhood,p0):
    """
    computes the least mean squares solution for points in neighborhood.
    p0 is the initial guess for (a,b,c)
    returns a,b,c for the local minima found.
    """
    if neighborhood.shape[0]<5:
        return None
    sol = leastsq(residuals, p0, args=(None, neighborhood.T))[0]
    return sol

def f_min(X, p):
    """
    plane function to minimize.
    """
    ab = p[0:2]
    distance = (ab*X[:2].T).sum(axis=1) + p[2] - X[2]
    return distance

def residuals(params, signal, X):
    """
    residuals for least mean squares
    """
    return f_min(X, params)



p0 = np.random.uniform(-50, 50, size=(3,1))
sol = least_squares(neighborhood,p0)

最佳答案

这是一种方法: 给定 N X,Y,Z 值,您希望找到 a,b,c,d 来最小化

Q = Sum{ i | square( (a,b,c)*(X[i], Y[i], Z[i])' - d)}

无论您为 (a,b,c) 选择什么值,最小化该值的 d 值将是

d = Sum{ i | (a,b,c)*(X[i], Y[i], Z[i])' }/N
  = (a,b,c)*(Xbar,Ybar,Zvar)

其中 Xbar 是 X 等的平均值。

将其代入 Q 的表达式,我们得到

Q = Sum{ i | square( (a,b,c)*(x[i], y[i], x[i])')} 
  = (a,b,c)*Sum{ i | (x[i], y[i], x[i])' * (x[i], y[i], x[i])}*(a,b,c)'
  = (a,b,c)*M*(a,b,c)'

其中 x[i] = X[i]-xbar, y[i]=Y[i]-ybar 等等

M = Sum{ i | (x[i], y[i], x[i])' * (x[i], y[i], x[i])}

归一化 (a,b,c) 的 q 最小值将是 M 的最小特征向量,然后 (a,b,c) 将是该特征值的特征向量。

所以程序是:

a/计算坐标的 xbar、ybar、zbar 平均值,并从 x[]、y[]、z[] 中减去它们

b/构造矩阵 M 并将其对角化

c/M 的最低特征值的特征向量给出 (a,b,c)

d/通过 计算 d

d = (a,b,c)*(xbar,ybar,zbar)'

关于python - 使用归一化系数进行平面拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38215223/

相关文章:

python - 在 python 循环中运行子进程

python - 根据多个条件计算 ndarray 中某个项目的出现次数?

python - 如何为 Scipy 的 csr_matrix 指定行名和列名?

c++ - 将复杂的链接标志从 Makefile 传递到 setup.py

python - python 中使用十六进制值的 bytearray

python - 如何高效使用numpy进行迭代求和

python - R 优化与 Scipy 优化之间的差异 : Nelder-Mead

python - 如何在 numpy/scipy 中实现像素数学

python - 如何更好地利用缓存进行 pip 安装

python - 使用 pandas groupby 时数据丢失时 np.average 不起作用