python - 如何将二维查找表映射到数组(python)?

标签 python numpy lookup-tables

我有三个相同形状的二维数组,我们称它们为 theta、phi 和 A。 令 theta 和 phi 为从表面上不同距离看到的法向量的角度:

size = 100 # this value is fixed
x = np.arange(-size, size)
y = np.arange(-size, size)
xx, yy = np.meshgrid(xx, yy)
theta = np.arctan((xx**2+yy**2)**0.5 / 100) # angle from distance 100
phi = np.arctan((xx**2+yy**2)**0.5 / 1000) # angle from distance 1000

并设 A 为测量值的二维图,其中 x 轴为 theta,y 轴为已知线性步长的 phi(实际上与 theta 和 phi 的形状不同)。我需要的是表示为 A(x,y) 的 A(theta,phi) 的值。 看来我不知道如何将 A(theta,phi) 转换为 A(x,y),即使我知道 theta(x,y) 和 phi(x,y)。

我尝试过的: 通过 scipy.interpolate.interp2d 我可以将 A 映射到与 theta 和 phi 相同数量的行和列。现在我可以遍历索引并猜测/舍入数组中最匹配的索引

B = np.zeros(A.shape)
for i in range(0,A.shape[0]):
  for j in range(0,A.shape[1]):
    B[i,j] = A[int(f_theta*theta[i,j]),int(f_phi*phi[i,j])]

其中 f_theta 和 f_phi 是由索引步的测量步长确定的预因子。 对我来说,这看起来是非常糟糕和低效的编码,并且像是对我实际想要做的事情的粗略近似(这是一个逆插值映射?)。它让我想起查找表、坐标变换和插值,但由于没有这些关键字,我找到了适合解决问题的方法。 我的 python 经验告诉我会有一个我不知道的模块/函数。

编辑限制: A(theta, phi) 中轴的范围大于 theta(x,y) 和 phi(x,y) 的范围,因此映射值始终存在。 我不需要将 B 映射回 A,因此不存在缺失值问题。 映射 A(theta, phi) 中的许多值将永远不会被使用。

编辑清晰度: 我将举一个小矩阵的例子,希望能澄清一些事情:

# phi given in degrees
phi = np.array([
    [2,1,2],
    [1,0,1],
    [2,1,2],
])
# theta given in degrees
theta = np.array([
    [6,4,6],
    [4,0,5],
    [6,5,6],
])
# A[0,0] is the value at phi=0deg, theta=0deg
# A[0,1] is the value at phi=1deg, theta=0deg
# A[1,1] is the value at phi=1deg, theta=1deg etc
# this is a toy example, the actual A cannot be constructed by a simple rule
A = np.array([
    [0.0,0.1,0.2],
    [1.0,1.1,1.2],
    [2.0,2.1,2.2],
    [3.0,3.1,3.2],
    [4.0,4.1,4.2],
    [5.0,5.1,5.2],
    [6.0,6.1,6.2],
])
# what I want to reach:
B = [[6.2,4.1,6.2],
     [4.1,0.0,5.1],
     [6.2,5.1,6.2]]

我需要澄清一下,我在这里做了一些简化:

1) 对于给定的 theta,我可以通过查看表格来检查相应的 phi: theta[i,j] 对应于 phi[i,j]。但是这个例子的构造太简单了,他们没有,例如共享相同的来源,它是嘈杂的数据,因此我无法给出分析表达式 theta(phi) 或 phi(theta)

2) 我的实际 theta 和 phi 中的值是 float ,我的实际 A 也在非整数步长中测量(例如,theta 方向每步 0.45 度,phi 方向每步 0.2 度)

3) 原则上,由于 theta 和 phi 之间存在严格的关系,我只需要值 A 的特定一维“轨迹”即可找到 B,但我不明白如何找到此轨迹,也不知道如何创建B出踪迹。示例中的此轨迹为 [A[0,0],A[4,1],A[5,1],A[6,2]] = [0.0,4.1,5.1,6.2]

最佳答案

例如,您可以执行双线性插值:

from scipy.interpolate import interpn

delta = [1.0, 1.0] # theta, phi
points = [np.arange(s)*d for s, d in zip(A.shape, delta)]
xi = np.stack((theta, phi), axis = -1)
B = interpn(points, A, xi)

这给出:

print(B)
[[6.2 4.1 6.2]
 [4.1 0.  5.1]
 [6.2 5.1 6.2]]

关于python - 如何将二维查找表映射到数组(python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60298076/

相关文章:

python - 在 matplotlib 中迭代添加总数未知的子图

python - 将 n 列添加到 numpy 数组

Python 导入错误 : cannot import name 'six' from 'sklearn.externals'

grails - GORM/Grails - 向 joinTable 表达式添加额外的列

python - PyCharm:导航到最近的方法?

python - Pickle Tfidfvectorizer 以及自定义分词器

python - 在正则表达式中使用非贪婪限定符 '*?'

python - 如何在Python中找到多列中重复行的最大绝对值并显示其行和列索引

c# - 在 C# 程序中嵌入包含值的静态表

Java 编译器错误 : lookup table exceeds 65535 limit