Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在非均匀二维栅格上插值缺失值_Python_Arrays_Interpolation - Fatal编程技术网

Python 在非均匀二维栅格上插值缺失值

Python 在非均匀二维栅格上插值缺失值,python,arrays,interpolation,Python,Arrays,Interpolation,我试图在Python中插入2D数组缺少的值。然而,我发现在这种情况下,行和列都是等距的。在我的例子中,我有两个数组 x = [275. 290. 310. 330. 350. 410. 450.] y = [ 8. 12. 16. 20. 30. 35. 40. 45.] 其中x和y是表示二维数组所在列和行节点的栅格坐标 c = [[4 6 9 9 9 8 2] [1 6 3 7 1 5 4] [8 nan 3 nan 2 9 2] [8 2

我试图在Python中插入2D数组缺少的值。然而,我发现在这种情况下,行和列都是等距的。在我的例子中,我有两个数组

x = [275. 290. 310. 330. 350. 410. 450.]
y = [ 8. 12. 16. 20. 30. 35. 40. 45.]
其中
x
y
是表示二维数组所在列和行节点的栅格坐标

c = [[4 6   9 9   9 8 2]
     [1 6   3 7   1 5 4]
     [8 nan 3 nan 2 9 2]
     [8 2   3 4   3 4 7]
     [2 nan 4 nan 6 1 3]
     [4 nan 8 nan 1 7 6]
     [8 nan 6 nan 5 6 5]
     [1 nan 1 nan 3 1 9]]
定义了


填充缺失值的最佳方法是什么?

scipy包括栅格数据的2D插值(还有一些其他插值函数):


你的意思是
a
b
是网格坐标吗?为什么不叫他们
x
y
?是的,这就是我的意思。我现在就给他们改名!你链接的问题也有用吗?只需从这里替换
x
y
import numpy as np
import pandas as pd
from numpy import nan
from scipy.interpolate import griddata

x = [275, 290, 310, 330, 350, 410, 450]
y = [ 8, 12, 16, 20, 30, 35, 40, 45,]
c = np.array([[ 4.,  6.,  9.,  9.,  9.,  8.,  2.],
       [ 1.,  6.,  3.,  7.,  1.,  5.,  4.],
       [ 8., nan,  3., nan,  2.,  9.,  2.],
       [ 8.,  2.,  3.,  4.,  3.,  4.,  7.],
       [ 2., nan,  4., nan,  6.,  1.,  3.],
       [ 4., nan,  8., nan,  1.,  7.,  6.],
       [ 8., nan,  6., nan,  5.,  6.,  5.],
       [ 1., nan,  1., nan,  3.,  1.,  9.]])

# generate x_coord, y_coord values for each grid point
x_grid, y_grid = np.meshgrid(x,y)

# get known values to set the interpolator
mask = [~np.isnan(c)]
x = x_grid[mask].reshape(-1)
y = y_grid[mask].reshape(-1)
points = np.array([x,y]).T
values = c[mask].reshape(-1) 

# generate interpolated grid data
interp_grid = griddata(points, values, (x_grid, y_grid), method='nearest')

# interp grid:

array([[4., 6., 9., 9., 9., 8., 2.],
       [1., 6., 3., 7., 1., 5., 4.],
       [8., 6., 3., 7., 2., 9., 2.],
       [8., 2., 3., 4., 3., 4., 7.],
       [2., 2., 4., 4., 6., 1., 3.],
       [4., 4., 8., 4., 1., 7., 6.],
       [8., 8., 6., 6., 5., 6., 5.],
       [1., 1., 1., 1., 3., 1., 9.]])