Bor*_*rys 13 python arrays lookup performance numpy
LUT = np.genfromtxt('test.out', delimiter=',', dtype=float)
LUT:
12, 25, 136, 6743
13, 26, 139, 6786
14, 27, 142, 6791
15, 28, 145, 6789
Run Code Online (Sandbox Code Playgroud)
要从LUT读取的值如下:
x1, x2, x3 = 12.5, 25.5, 137
Run Code Online (Sandbox Code Playgroud)
对于每个给定值(3列)读取LUT中的相邻两个值,我必须对结果进行线性插值(LUT中的第4列).
给定值(x1,x2,x3)属于LUT的第1行和第2行之间.基于此如何读取第1行和第2行之间的结果?
给定coords
要插值的坐标列表,可以使用scipy.spatial.cKDTree
获得表格的2个最接近的条目,这些条目是线性插值所必需的.下面的代码显示了一个已经向量化的用法示例.
import numpy as np
from scipy.spatial import cKDTree
# inputs
LTU = np.genfromtxt('test.txt', delimiter=',')
coords = ((12.5, 25.5, 137),
(13.5, 26.5, 141),
(14.5, 25.5, 144))
# querying and interpolating
xyz = LTU[:, :3]
val = LTU[:, 3]
del LTU # attempt to clean up memory
tree = cKDTree(xyz)
dist, ind = tree.query(coords, k=2)
d1, d2 = dist.T
v1, v2 = val[ind].T
v = (d1)/(d1 + d2)*(v2 - v1) + v1
print(v)
#[ 6758.73909236 6789.16987298 6790.03575996]
Run Code Online (Sandbox Code Playgroud)