如何在python中插入数据?

时间:2017-01-12 10:08:54

标签: python scipy interpolation

我有一个4D数据集(time, z, y, x),我想插入数据以获得更高的分辨率,这是一个简单的示例代码:

import numpy as np
from scipy.interpolate import griddata

x_0 = 10
cut_index = 10

res = 200j
x_index = x_0
y_index = np.linspace(0, 100, 50).astype(int)
z_index = np.linspace(0, 50, 25).astype(int)

#Time, zyx-coordinate
u = np.random.randn(20, 110, 110, 110)

z_index, y_index = np.meshgrid(z_index, y_index)
data = u[cut_index, z_index, y_index, x_index]


res = 200j
y_f = np.mgrid[0:100:res]
z_f = np.mgrid[0:50:res]
z_f, y_f = np.meshgrid(z_f, y_f)

data = griddata((z_index, y_index), data, (z_f, y_f))

我收到ValueError: invalid shape for input data points错误。 griddata函数需要什么样的输入?

1 个答案:

答案 0 :(得分:1)

您的data参数必须是一维数组。尝试展平数组:

data = griddata((z_index.flatten(), y_index.flatten()), data.flatten(), (z_f, y_f))