Python 两个数组上ckdtree搜索的矢量化循环
我有一个csv文件,其中包含随机位置的纬度、经度和高程值。我想应用IDW插值来生成规则网格。我使用scipy.spatial.cKDTree进行最近邻搜索,并在未知点查找高程值。 当输出栅格具有尺寸(z<1000 X1000)时,以下代码可以正常工作。如果维数增加,代码运行速度会非常慢。请帮助我使用cKDTree将for循环矢量化,而不删除。多谢各位Python 两个数组上ckdtree搜索的矢量化循环,python,numpy,vectorization,kdtree,Python,Numpy,Vectorization,Kdtree,我有一个csv文件,其中包含随机位置的纬度、经度和高程值。我想应用IDW插值来生成规则网格。我使用scipy.spatial.cKDTree进行最近邻搜索,并在未知点查找高程值。 当输出栅格具有尺寸(z
## Inverse distance weighted function
def idw(p, dist, values):
dist_pow = np.power(dist, 2)
nominator = np.sum(values/dist_pow)
denominator = np.sum(1/dist_pow)
if denominator > 0:
return nominator/denominator
else:
return none
## Reading the lat/lon and elevation values from file
lat = []
lon = []
ele = []
with open('VSKP_ground_dat.csv') as read:
csvreader = csv.DictReader(read)
for row in csvreader:
lat.append(float(row['LAT']))
lon.append(float(row['LON']))
ele.append(float(row['ALT']))
xycoord = np.c_[lon,lat]
ele_arr = np.array(ele)
## ------------- Creating KDTree
point_tree = spatial.cKDTree(xycoord, leafsize=25)
## ------------- Creating empty grid matrix with np.zeros
xmin, xmax, ymin, ymax = 81.903158, 83.352158, 17.25856, 18.40056
## --------- Defining resolution
xres, yres = 0.01, 0.01
x = np.arange(xmin, xmax, xres)
y = np.arange(ymin, ymax, yres)
z = np.zeros((x.shape[0], y.shape[0]), dtype=np.float16)
for i, val1 in enumerate(x):
for j, val2 in enumerate(y):
p = np.array([val1, val2])
# points_idx = point_tree.query_ball_point(p, dist_2)
distances, points_idx = point_tree.query(p, k=6, eps=0)
ele_vals = ele_arr[points_idx]
value = idw(p, distances, ele_vals)
z[i,j] = value
首先,将
idw
函数设置为在上一个索引上工作:
def idw(dist, values, p = 2):
out = np.empty(dist.shape[:-1])
mask = np.isclose(dist, 0).any(-1)
out[mask] = values[np.isclose(dist, 0)] # should be only one per point
dist_pow = np.power(dist[~mask], -p) # division is costly, do it once
nominator = np.sum(values[~mask] * dist_pow, axis = -1) # over mask to prevent divide by zero
denominator = np.sum(dist_pow, index = -1)
out[~mask] = nominator / denominator
return out
然后根据np.meshgrid
输出执行其余操作
x = np.arange(xmin, xmax, xres) # len i
y = np.arange(ymin, ymax, yres) # len j
xy = np.stack(np.meshgrid(x, y), axis = -1) # shape(i, j, 2)
distances, points_idx = point_tree.query(xy, k=6, eps=0) # shape (i, j, 6)
ele_vals = ele_arr[points_idx] # shape (i, j, 6)
z = idw(distances, ele_vals) # shape (i, j)
什么是
idw
功能?您需要将其矢量化,以删除循环的,因此请检查并确保变量名称一致。我假设p
和point
应该是同一件事?谢谢你的回复。是的,P和点是相同的(我的错误),IDW是反距离加权插值函数。你有这个函数的代码吗?你能把它编辑成问题吗?(同样,编辑你的代码要正确,编辑问题中的代码是不受欢迎的,除非发问者)我已经添加了这个功能并更正了循环中的拼写错误。使用cKDTree的循环花费了很长时间。我正在获取spatial.cKDTree.cKDTree.query value错误:x必须由长度为2的向量组成,但具有形状(2,115,145)。我采用了lat=[17.749405,17.788955,17.805596,17.817568,…]
和lon=[81.903658,81.940228,81.97914,82.008435,82.009833,82.03696,…]
是的,忘记了np.stack
中的轴以获得正确的形状。很抱歉这很有效。请将IDW功能中的索引
更改为轴
,如果可能,请详细解释IDW功能。谢谢你的帮助。