[英]interpolate missing values 2d python
我有一个二维数组(或矩阵,如果您愿意),其中一些缺失值表示为NaN
。 缺失值通常位于一个轴上的条带中,例如:
1 2 3 NaN 5
2 3 4 Nan 6
3 4 Nan Nan 7
4 5 Nan Nan 8
5 6 7 8 9
我想用一些合理的数字替换NaN
。
我研究了 delaunay 三角剖分,但发现的文档很少。
我尝试使用astropy
的卷积,因为它支持使用二维数组,而且非常简单。 问题在于卷积不是插值,它会将所有值移向平均值(可以通过使用窄内核来缓解)。
这个问题应该是这个帖子的自然二维扩展。 有没有办法在二维数组中插入NaN
/缺失值?
是的,您可以使用scipy.interpolate.griddata
和掩码数组,您可以使用参数method
选择您喜欢的插值类型,通常'cubic'
做得很好:
import numpy as np
from scipy import interpolate
#Let's create some random data
array = np.random.random_integers(0,10,(10,10)).astype(float)
#values grater then 7 goes to np.nan
array[array>7] = np.nan
使用plt.imshow(array,interpolation='nearest')
看起来像这样:
x = np.arange(0, array.shape[1])
y = np.arange(0, array.shape[0])
#mask invalid values
array = np.ma.masked_invalid(array)
xx, yy = np.meshgrid(x, y)
#get only the valid values
x1 = xx[~array.mask]
y1 = yy[~array.mask]
newarr = array[~array.mask]
GD1 = interpolate.griddata((x1, y1), newarr.ravel(),
(xx, yy),
method='cubic')
这是最终结果:
看看,如果 nan 值在边缘并且被 nan 值包围,则无法插入并保持nan
。 您可以使用fill_value
参数更改它。
这取决于您的数据类型,您必须进行一些测试。 例如,您可以故意屏蔽一些好的数据,尝试使用具有屏蔽值的数组进行不同类型的插值,例如三次、线性等,并计算内插值与您之前屏蔽的原始值之间的差异,然后查看哪个方法返回您的细微差别。
你可以使用这样的东西:
reference = array[3:6,3:6].copy()
array[3:6,3:6] = np.nan
method = ['linear', 'nearest', 'cubic']
for i in method:
GD1 = interpolate.griddata((x1, y1), newarr.ravel(),
(xx, yy),
method=i)
meandifference = np.mean(np.abs(reference - GD1[3:6,3:6]))
print ' %s interpolation difference: %s' %(i,meandifference )
这给出了这样的东西:
linear interpolation difference: 4.88888888889
nearest interpolation difference: 4.11111111111
cubic interpolation difference: 5.99400137377
当然,这是针对随机数的,因此结果可能会有很大差异是正常的。 因此,最好的办法是对数据集的“故意屏蔽”部分进行测试,看看会发生什么。
为方便起见,这里有一个实现GM 答案的函数。
from scipy import interpolate
import numpy as np
def interpolate_missing_pixels(
image: np.ndarray,
mask: np.ndarray,
method: str = 'nearest',
fill_value: int = 0
):
"""
:param image: a 2D image
:param mask: a 2D boolean image, True indicates missing values
:param method: interpolation method, one of
'nearest', 'linear', 'cubic'.
:param fill_value: which value to use for filling up data outside the
convex hull of known pixel values.
Default is 0, Has no effect for 'nearest'.
:return: the image with missing values interpolated
"""
from scipy import interpolate
h, w = image.shape[:2]
xx, yy = np.meshgrid(np.arange(w), np.arange(h))
known_x = xx[~mask]
known_y = yy[~mask]
known_v = image[~mask]
missing_x = xx[mask]
missing_y = yy[mask]
interp_values = interpolate.griddata(
(known_x, known_y), known_v, (missing_x, missing_y),
method=method, fill_value=fill_value
)
interp_image = image.copy()
interp_image[missing_y, missing_x] = interp_values
return interp_image
我实际上会逐行手动检查这个矩阵,每当你开始遇到一个 Nans 列表时,跟踪紧接在 Nans 之前和紧接其后的数字,以及在返回普通数字之前看到的 Nans 的数量。 一旦找到这些数字,就可以自己用内插值覆盖 Nans。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.