插值缺失值2d python [英] 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
的卷积,因为它支持使用2d数组,非常简单.
问题在于卷积不是插值,而是将所有值都移向平均值(可以通过使用窄核来缓解).
这个问题应该是这篇文章
的自然二维扩展.一个>.有没有一种方法可以对2d数组中的NaN
/缺失值进行插值?
是的,您可以使用 I have a 2d array(or matrix if you prefer) with some missing values represented as
where I would like to replace the I looked into delaunay triangulation, but found very little documentation. I tried using This question should be the natural 2-dimensional extension to this post. Is there a way to interpolate over Yes you can use That looks something like this using This is the final result: Look that if the nan values are in the edges and are surrounded by nan values thay can't be interpolated and are kept It depends on your kind of data, you have to perform some test. You could for instance mask on purpose some good data try different kind of interpolation e.g. cubic, linear etc. etc. with the array with the masked values and calculuate the difference between the values interpolated and the original values that you had masked before and see which method return you the minor difference. You can use something like this: That gives something like this: Of course this is for random numbers so it's normal that the result may vary a lot. So the best thing to do is to test on "on purpose masked" piece of your dataset and see what happen. 这篇关于插值缺失值2d python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!NaN
. The missing values are typically in a strip along one axis, eg: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
's by somewhat sensible numbers. astropy
's convolve as it supports use of 2d arrays, and is quite straightforward.
The problem with this is that convolution is not interpolation, it moves all values towards the average (which could be mitigated by using a narrow kernel).NaN
/missing values in a 2d-array? scipy.interpolate.griddata
and masked array and you can choose the type of interpolation that you prefer using the argument method
usually 'cubic'
do an excellent job: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
. You can change it using the fill_value
argument.How would this work if there is a 3x3 region of NaN-values, would you get sensible data for the middle point?
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