Ale*_*lex 5 python statistics optimization numpy scipy
我正在进行本地化项目并使用最小二乘估计来确定发射机的位置.我需要一种方法来统计我的程序中我的解决方案的"适应性",这可以用来告诉我是否有一个好的答案,或者我需要额外的测量,或者有不好的数据.我已经阅读了一些关于使用"确定系数"或R平方的内容,但未能找到任何好的例子.关于如何表征我是否有一个好的解决方案,或需要额外的测量的任何想法将非常感激.
谢谢!
我的代码给了我以下输出,
grid_lat和grid_lon对应于可能的目标位置的网格的纬度和经度坐标
grid_lat = [[ 38.16755799 38.16755799 38.16755799 38.16755799 38.16755799
38.16755799]
[ 38.17717199 38.17717199 38.17717199 38.17717199 38.17717199
38.17717199]
[ 38.186786 38.186786 38.186786 38.186786 38.186786 38.186786 ]
[ 38.1964 38.1964 38.1964 38.1964 38.1964 38.1964 ]
[ 38.20601401 38.20601401 38.20601401 38.20601401 38.20601401
38.20601401]
[ 38.21562801 38.21562801 38.21562801 38.21562801 38.21562801
38.21562801]
[ 38.22524202 38.22524202 38.22524202 38.22524202 38.22524202
38.22524202]]
grid_lon = [[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]]
Run Code Online (Sandbox Code Playgroud)
grid_error对应于每个点的解决方案的"好"程度.如果我们有0.0的误差,我们有一个完美的解决方案.针对每个测量位置(下面的测量中的轨迹)计算网格上的每个点的网格误差.每个测量位置具有到发射器的估计范围."误差"对应于来自测量的发射器的估计范围,减去在测量范围位置和网格点之间计算的实际范围.误差越小,我们接近实际发射机位置的机会就越大
# Calculate distance between every grid point and every measurement in meters
measured_distance = spatial.distance.cdist(grid_ecef_array, measurement_ecef_array, 'euclidean')
measurement_error = [pow((measurement - estimated_distance),2) for measurement in measured_distance]
mean_squared_error = [numpy.sqrt(numpy.mean(measurement)) for measurement in measurement_error]
# Find minimum solution
# Convert array of mean_squared_errors to 2D grid for graphing
N3, N4 = numpy.array(grid_lon).shape
grid_error = numpy.array(mean_squared_error).reshape((N3, N4))
grid_error = [[ 2.33608445 2.02805063 1.85638288 1.84620283 2.02757163 2.38035108]
[ 1.73675429 1.40649524 1.21799211 1.06503271 1.27373554 1.74265406]
[ 1.44967789 0.96835022 0.62667257 0.52804942 0.91189678 1.50067864]
[ 1.70155286 1.24024402 0.9642869 1.00517531 1.32606411 1.81754752]
[ 2.40218247 2.07449106 1.91044903 1.94272889 2.15511638 2.51683715]
[ 3.29679348 3.05353929 2.93662134 2.95839307 3.11583615 3.39320682]
[ 4.27303679 4.08195869 3.99203754 4.00926823 4.13247105 4.35378011]]
# Generate the 3D plot with the Z coordinate being the mean squared error estimate
plot3Dcoordinates(grid_lon, grid_lat, grid_error)
# Generic function using matplotlib to plot coordinates
def plot3Dcoordinates(X, Y, Z):
fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
Run Code Online (Sandbox Code Playgroud)
这是在更大的网格上处理算法的示例图像.我可以直观地看出我有一个非常好的解决方案,因为形状平滑地收敛于一个最小点(解决方案),看起来有点像倒置的巫婆帽子.

第二幅图像显示所有测量和位置,溶液绘制在顶部,最小点作为解决方案(红色x).
