我正在尝试使用 scipy.optimize.leastsq 将3d点分配到2.5d / 3d的平面。
我想尽量减少这个功能:a x + b y + 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]
[-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)
答案 0 :(得分:1)
这是一种方式: 给定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)'