python - Numpy/Python 中的快速性能数组处理

标签 python arrays numpy grid

我正在尝试找出最佳方式(最快的性能)来处理存储在多个 numpy 数组中的坐标和测量数据。

我需要计算每个网格点(附加图像中绿色的地段、经度、alt 值)到每个测量位置(lat、lon、alt、附加图像中灰色目标的范围)的距离。鉴于有数百个网格点,每个网格点要计算数千个测量范围,我想以最有效的方式遍历数组

enter image description here

我试图在如何存储网格的 LLA 测量值和测量值之间做出决定,然后根据测量范围值和测量值之间的差值计算网格上每个点的均方误差的理想方法是什么实际范围。

对于如何最好地存储这些值,然后遍历网格以确定每次测量的范围的任何想法,我们将不胜感激。谢谢!!!

目前,我正在使用 2D 网格来存储网格的 LLA 值

# Create a 2D Grid that will be used to store the MSE estimations
# First, create two 1-D arrays representing the X and Y coordinates of our grid
x_delta = abs(xmax-xmin)/gridsize_x
y_delta = abs(ymax-ymin)/gridsize_y
X = np.arange(xmin,xmax+x_delta,x_delta)
Y = np.arange(ymin,ymax+y_delta,y_delta)

# Next, pass arrays to meshgrid to return 2-D coordinate matrices from the 1-D coordinate arrays
grid_lon, grid_lat = np.meshgrid(X, Y)

我有存储在测量类中的测量的 LLA 点和范围值

measurement_lon = [measurement.gps.getlon() for measurement in target_measurements]
measurement_lat = [measurement.gps.getlat() for measurement in target_measurements]
measurement_range = [measurement.getrange() for measurement in target_measurements]

测量类

class RangeMeasurement:

def __init__(self, lat, lon, alt, range):
  self.gps = GpsLocation(lat,lon,alt)
  self.range = range

非常糟糕的范围计算伪代码(迭代且非常慢)

for i in len(grid_lon):
  for j in len(measurement_lat):
    range_error += distance(grid_lon[i],grid_lat[i],measurement_lon[j],measurement_lat[j])-measurement_range[j]      

最佳答案

我认为 scipy.spatial.distance 模块可以帮助您解决这个问题:http://docs.scipy.org/doc/scipy/reference/spatial.distance.html

您应该将点存储为具有 2 列和 N 行的二维 numpy 数组,其中 N 是数组中点的数量。要将您的 grid_lon 和 grid_lat 转换为这种格式,请使用

N1 = grid_lon.size
grid_point_array = np.hstack([grid_lon.reshape((N1,1)), grid_lat.reshape((N1,1))])

这里将grid_lon中的所有值,排列成与grid形状相同的矩形数组,放入一个一列N行的数组中。它对 grid_lat 做同样的事情。然后将两个单列宽数组组合起来创建一个两列数组。

可以使用类似的方法来转换您的测量数据:

N2 = len(measurement_lon)
measurment_data_array = np.hstack([np.array(measurement_lon).reshape((N2,1)),
    np.array(measurement_lat).reshape((N2,1))])

一旦您的数据采用这种格式,您就可以使用 scipy.spatial.distance 轻松找到每对点之间的距离:

d = scipy.spatial.distance.cdist(grid_point_array, measurement_data_array, 'euclidean')

d是一个N1行N2列的数组,d[i,j]是网格点i到测量点j的距离。

编辑感谢您澄清范围错误。听起来是个有趣的项目。这应该为您提供累积平方误差最小的网格点:

measurement_range_array = np.array(measurement_range)
flat_grid_idx = pow(measurement_range_array-d,2).sum(1).argmin()

这利用了 broadcasting得到一个点的测量范围和它到每个网格点的距离之间的差异。然后对给定网格点的所有错误求和,生成的一维数组应该是您要查找的累积错误。调用 argmin() 以查找最小值的位置。要从展平索引中获取 x 和 y 网格坐标,请使用

grid_x = flat_grid_idx % gridsize_x
grid_y = flat_grid_idx // gridsize_x

(//是整数除法。)

关于python - Numpy/Python 中的快速性能数组处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8406107/

相关文章:

c++ - UTF-8 编码错误,需要帮助转换文本

javascript - 从对象内的数组获取值,范围问题

python - Numpy:获取二维数组最小值的列索引和行索引

python - "painting"使用 python/numpy 将一个数组转换为另一个数组

Python Pandas 在函数中处理数据帧

python - 如何在单个声明中创建多个空类?

python - 被中间件执行过程迷惑,DJANGO

java - 从数组中抓取单词

python - 在一维 numpy 数组中使用 Numpy 查找高于某个阈值的最大值

python - 使用 matplotlib 绘制分段函数会导致 ValueError : The truth value of an array with more than one element is ambiguous