gpt4 book ai didi

python - Scipy Griddata 输出维度

转载 作者:行者123 更新时间:2023-11-28 18:45:04 25 4
gpt4 key购买 nike

我不确定我做错了什么。我正在尝试使用 scipy griddata 在不规则网格中插入数据。

from scipy.interpolate import griddata

我有两个列表,“x”和“y”,它们代表我原始的、未插值的网格的轴。它们都是长度为 8 的列表。

然后,我创建了代表预期最终填充网格轴的数组。

ny = np.linspace(0.0, max(y), y[len(y)-1]/min_interval+1)
nx = np.linspace(0.0, max(x), len(ny))

我检查过,“ny”和“nx”的形状都是 (61,)。然后,我创建一个 8 x 8 列表“z”。最后,我尝试制作我的最终网格。

Z = griddata((np.array(x), np.array(y)), np.array(z), (nx, ny), method='nearest', fill_value=0)
print Z.shape

生成的二维数组的维度为 (61,8)。我尝试使用“x”和“y”作为列表和数组 - 没有变化。为什么它只在一个方向上插值?我期待 (61,61) 数组输出。如果我觉得有用的话,我会包括实际数字,但我看不出它会有什么不同。我不明白 griddata 是如何工作的吗?

最佳答案

完整代码如下:

import numpy as np
from scipy.interpolate import griddata

# random data to interpolate
x = np.array([0, 10, 13, 17, 20, 50, 55, 60.0])
y = np.array([10, 20, 40, 80, 90, 95, 100, 120.0])
zg = np.random.randn(8, 8)

#select one of the following two line, it depends on the order in z
#xg, yg = np.broadcast_arrays(x[:, None], y[None, :])
xg, yg = np.broadcast_arrays(x[None, :], y[:, None])

yg2, xg2 = np.mgrid[y.min()-10:y.max()+10:100j, x.min()-10:x.max()+10:100j]

zg2 = griddata((xg.ravel(), yg.ravel()), zg.ravel(), (xg2.ravel(), yg2.ravel()), method="nearest")
zg2.shape = yg2.shape

import pylab as pl

pl.pcolormesh(xg2, yg2, zg2)
pl.scatter(xg.ravel(), yg.ravel(), c=zg.ravel())

输出是:

enter image description here

关于python - Scipy Griddata 输出维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21591431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com