【发布时间】:2022-01-03 08:08:33
【问题描述】:
目标
我在特定的 3D 坐标 x y z 处给出了值 v。数据存储为 pandas 数据框:
x y z v
0 -68.5 68.50 -10.00 0.297845
1 -68.5 -23.29 61.10 0.148683
2 -68.5 -23.29 63.47 0.142325
3 -68.5 -23.29 65.84 0.135908
4 -68.5 -23.29 68.21 0.129365
... ... ... ...
91804 68.5 23.29 151.16 0.118460
91805 68.5 23.29 153.53 0.119462
91806 68.5 23.29 155.90 0.120386
91807 68.5 23.29 139.31 0.112257
91808 68.5 -68.50 227.00 0.127948
我想在不属于数据框的新坐标处找到值,因此我正在研究如何有效地插入数据。
我做了什么:
由于坐标在网格上,我可以使用interpn:
import numpy as np
from scipy.interpolate import interpn
# Extract the list of coordinates (I know that they are on a grid)
xs = np.array(df["x"].to_list())
ys = np.array(df["y"].to_list())
zs = np.array(df["z"].to_list())
# Extract the associated values
vs = np.array(df["v"].to_list())
重塑数据以适应 scipy 函数:
points = (np.unique(xs), np.unique(ys), np.unique(zs))
values= vs.reshape(len(np.unique(xs)), len(np.unique(ys)), len(np.unique(zs)))
为了测试插值,如果我输入与原始点相同的点,我想看看是否得到相同的值:
request = (xs,ys,zs)
output = interpn(points, values, request)
... 但是
我在想,我做错了什么??
其他:
数据集
请在此处找到完整的数据集:https://filebin.net/u10lrw956enqhg5i
可视化
from mayavi import mlab
# Create figure
fig = mlab.figure(1, fgcolor=(0, 0, 0), bgcolor=(0, 0, 0))
mlab.points3d(xs,ys,zs,output)
mlab.view(azimuth=270, elevation=90, roll=180, figure=fig)
# View plot
mlab.show()
【问题讨论】:
-
我根据您显示的数据的 sn-p 质疑您重塑
values的有效性。尝试使用np.unique的return_inverse参数放置在网格上 -
@MadPhysicist 非常好。我也怀疑这一点,但现在,我不确定如何检查有效性或修复它。你有什么建议吗?
-
@jlandercy。这里太笼统了。网格上的插值在可用时要简单得多,而且可用。
标签: python numpy scipy interpolation