简体   繁体   English

如何在python中插入数据?

[英]How can I interpolate data in python?

I have a 4D dataset (time, z, y, x) and I would like to interpolate the data to get a higher resolution, this is a simple example code:我有一个 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))

I am getting the ValueError: invalid shape for input data points error.我收到ValueError: invalid shape for input data points错误。 What kind of input is expected by the griddata function? griddata函数需要什么样的输入?

Your data parameter has to be a 1D array.您的data参数必须是一维数组。 Try flattening the arrays:尝试展平数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM