查找另一个数据集中数据的对应关系

时间:2014-08-28 14:05:07

标签: python arrays numpy scipy kdtree

我有a catalogue of data,我想在 MCMC 代码中使用它。重要的是实施的速度,以避免减慢我的马尔可夫链monte carlo采样。 问题: 在目录中,我在第一和第二列中有两个名为radec的参数,它们是天空坐标

data=np.loadtxt('Final.Cluster.Shear.NegligibleShotNoise.Redshift.cat')
ra=data[:,0]
dec=data[:,1]

然后在七列和八列XY位置,即网格坐标,它们是网格空间中的点

Xpos=data[:,6]
Ypos=data[:,7]

在我写的函数中,需要调用一百万次, 我将给一个XcenterYcenter位置(例如Xcenter = 200.6,Ycenter = 310.9)作为函数的输入,我想找到ra和{{中的对应点1}}列。但是,输入可能在decra中没有真正的对应关系。所以我想进行插值,以防目录中的decX以及Yra数据没有相似的条目,并获得基于实际的插值坐标目录中的decra条目。

1 个答案:

答案 0 :(得分:1)

这是一个完美的案例,scipy.spatial.cKDTree()类可用于一次查询所有点:

from scipy.spatial import cKDTree

k = cKDTree(data[:, 6:8]) # creating the KDtree using the Xpos and Ypos

xyCenters = np.array([[200.6, 310.9],
                      [300, 300],
                      [400, 400]])
print(k.query(xyCenters))
# (array([ 1.59740195,  1.56033234,  0.56352196]),
#  array([ 2662, 22789,  5932]))

其中[ 2662, 22789, 5932]是与xyCenters中给出的三个最近点对应的索引。您可以使用这些索引使用ra非常有效地获取decnp.take()值:

dists, indices = k.query(xyCenters)
myra = np.take(ra, indices)
mydec = np.take(dec, indices)