python - 如何表征最小二乘估计的适应度

标签 python optimization numpy statistics 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]]

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)

这是在更大的网格上处理算法的示例图像。我可以从视觉上看出我有一个很好的解决方案,因为形状平滑地收敛在一个最小值点(解决方案)上,看起来有点像倒置的女巫帽。 enter image description here

第二张图片显示了所有测量值和位置,解决方案绘制在顶部,最小点作为解决方案(红色 x)。

enter image description here

最佳答案

R 平方越接近 1.0,您的拟合度就越好。选择您自己的“足够好”阈值,我认为典型的阈值在 .92-.98 范围内。

关于python - 如何表征最小二乘估计的适应度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512312/

相关文章:

python - 通过在Python中添加全局变量名称和字符串来创建df列名称

python - Django + (django-model-utils) : Combining two models/inheriting from two models

MySQL乘法子查询结果

optimization - 什么是Big O符号?你会用吗?

python - 以其他维度的关联值为条件对 3d numpy 数组的分配进行矢量化

python - 通过匹配不同列和不同行的值来创建新列 python pandas

Python 检查 unicode 文件

c++ - Visual C++ 逗号运算符和 sse 内在函数

python - 为什么 numpy 计算不受全局解释器锁的影响?

python - 强制输入总和为 1 并包含在 scikit-learn 的单位区间内