python⼀维数据插值_python-在numpy数组中插值NaN值python-在numpy数组中插值NaN值
有没有⼀种快速的⽅法⽤(例如)线性插值替换numpy数组中的所有NaN值?
例如,
[1 1 1 nan nan 2 2 nan 0]
将被转换成
[1 1 1 1.3 1.6 2 2 1 0]
9个解决⽅案
89 votes
让我们⾸先定义⼀个简单的辅助函数,以使其更直接地处理NaN的索引和逻辑索引:
import numpy as np
def nan_helper(y):
"""Helper to handle indices and logical indices of NaNs.
Input:
- y, 1d numpy array with possible NaNs
Output:
- nans, logical indices of NaNs
- index, a function, with signature indices= index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
return np.isnan(y), lambda z: z.nonzero()[0]
现在可以像下⾯这样利⽤nan_helper(.):
>>> y= array([1, 1, 1, NaN, NaN, 2, 2, NaN, 0])
>>>
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
>>>
>>> und(2)
[ 1. 1. 1. 1.33 1.67 2. 2. 1. 0. ]
---
尽管似乎先指定⼀个单独的函数来执⾏以下操作似乎有点过头了:
python 定义数组>>> nans, x= np.isnan(y), lambda z: z.nonzero()[0]
它最终将⽀付股息。
因此,每当您处理与NaNs相关的数据时,只需将其所需的所有(新的与NaN相关的新功能)封装在某些特定的辅助函数下即可。 您的代码库将更易于理解,因为它遵循易于理解的习惯⽤法。
插值确实是查看NaN处理如何完成的⼀个很好的上下⽂,但是在其他各种上下⽂中也使⽤了类似的技术。
eat answered 2020-01-01T02:00:42Z
22 votes
我想出了以下代码:
import numpy as np
nan = np.nan
A = np.array([1, nan, nan, 2, 2, nan, 0])
ok = -np.isnan(A)
xp = ok.ravel().nonzero()[0]
fp = A[-np.isnan(A)]
x = np.isnan(A).ravel().nonzero()[0]
A[np.isnan(A)] = np.interp(x, xp, fp)
print A
它打印
[ 1. 1.33333333 1.66666667 2. 2. 1. 0. ]
Petter answered 2020-01-01T02:01:06Z
9 votes
只需使⽤numpy逻辑,并在where语句中应⽤⼀维插值。
import numpy as np
from scipy import interpolate
def fill_nan(A):
'''
interpolate to fill nan values
'''
inds = np.arange(A.shape[0])
good = np.where(np.isfinite(A))
f = interpolate.interp1d(inds[good], A[good],bounds_error=False)
B = np.where(np.isfinite(A),A,f(inds))
return B
BRYAN WOODS answered 2020-01-01T02:01:26Z
5 votes
⾸先,更改数据的⽣成⽅式可能会更容易,但如果不是这样的话:
bad_indexes = np.isnan(data)
创建⼀个布尔数组,指⽰nan在哪⾥
good_indexes = np.logical_not(bad_indexes)
创建⼀个布尔数组,指⽰好的值区域在哪⾥
good_data = data[good_indexes]
原始数据的受限版本(不包括nans)
interpolated = np.interp(o(), o(), good_data)
通过插值运⾏所有不良索引
data[bad_indexes] = interpolated
⽤内插值替换原始数据。
Winston Ewert answered 2020-01-01T02:02:08Z
4 votes
或以温斯顿的答案为基础
def pad(data):
bad_indexes = np.isnan(data)
good_indexes = np.logical_not(bad_indexes)
good_data = data[good_indexes]
interpolated = np.interp(o()[0], o()[0], good_data) data[bad_indexes] = interpolated
return data
A = np.array([[1, 20, 300],
[nan, nan, nan],
[3, 40, 500]])
A = np.apply_along_axis(pad, 0, A)
print A
结果
[[ 1. 20. 300.]
[ 2. 30. 400.]
[ 3. 40. 500.]]
BBDynSys answered 2020-01-01T02:02:33Z
3 votes
对于⼆维数据,SciPy的griddata对我来说效果很好:
>>> import numpy as np
>>> from scipy.interpolate import griddata
>>>
>>> # SETUP
>>> a = np.arange(25).reshape((5, 5)).astype(float)
>>> a
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.],
[ 15., 16., 17., 18., 19.],
[ 20., 21., 22., 23., 24.]])
>>> a[np.random.randint(2, size=(5, 5)).astype(bool)] = np.NaN
>>> a
array([[ nan, nan, nan, 3., 4.],
[ nan, 6., 7., nan, nan],
[ 10., nan, nan, 13., nan],
[ 15., 16., 17., nan, 19.],
[ nan, nan, 22., 23., nan]])
>>>
>>> # THE INTERPOLATION
>>> x, y = np.indices(a.shape)
>>> interp = np.array(a)
>>> interp[np.isnan(interp)] = griddata(
... (x[~np.isnan(a)], y[~np.isnan(a)]), # points we know
... a[~np.isnan(a)], # values we know
... (x[np.isnan(a)], y[np.isnan(a)])) # points to interpolate
>>> interp
array([[ nan, nan, nan, 3., 4.],
[ nan, 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.],
[ 15., 16., 17., 18., 19.],
[ nan, nan, 22., 23., nan]])
我在3D图像上使⽤它,在2D切⽚(4000切⽚350x350)上运⾏。 整个操作⼤约需要⼀个⼩时:/
Gilly answered 2020-01-01T02:02:58Z
3 votes
我需要⼀种在数据末尾也填⼊NaN的⽅法,⽽主要答案似乎并没有这样做。
我想出的函数使⽤线性回归来填写NaN。 这解决了我的问题:
import numpy as np
def linearly_interpolate_nans(y):
# Fit a linear regression to the non-nan y values
# Create X matrix for linreg with an intercept and an index
X = np.vstack((np.ones(len(y)), np.arange(len(y))))
# Get the non-NaN values of X and y
X_fit = X[:, ~np.isnan(y)]
y_fit = y[~np.isnan(y)].reshape(-1, 1)
# Estimate the coefficients of the linear regression
beta = np.linalg.lstsq(X_fit.T, y_fit)[0]
# Fill in all the nan values using the predicted coefficients
y.flat[np.isnan(y)] = np.dot(X[:, np.isnan(y)].T, beta)
return y
这是⼀个⽰例使⽤案例:
# Make an array according to some linear function
y = np.arange(12) * 1.5 + 10.
# First and last value are NaN
y[0] = np.nan
y[-1] = np.nan
# 30% of other values are NaN
for i in range(len(y)):
if np.random.rand() > 0.7:
y[i] = np.nan
# NaN's are filled in!
print (y)
print (linearly_interpolate_nans(y))
nlml answered 2020-01-01T02:03:26Z
2 votes
在Bryan Woods的答案的基础上,我修改了他的代码,也将仅包含NaN的列表转换为零列表:def fill_nan(A):
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论