Interpoluj wartości NaN w tablicy numpy
Czy istnieje szybki sposób zastąpienia wszystkich wartości NaN w tablicy numpy (powiedzmy) wartościami interpolowanymi liniowo?
Na przykład,
[1 1 1 nan nan 2 2 nan 0]
Zostanie zamienione na
[1 1 1 1.3 1.6 2 2 1 0]
8 answers
Zdefiniujmy najpierw prostą funkcję pomocniczą, aby ułatwić obsługę indeksów i indeksów logicznych NaNs:
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]
Teraz nan_helper(.)
można teraz używać w następujący sposób:
>>> 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])
>>>
>>> print y.round(2)
[ 1. 1. 1. 1.33 1.67 2. 2. 1. 0. ]
---
Chociaż może się wydawać, że najpierw trochę przesadziłem, aby określić oddzielną funkcję do robienia takich rzeczy:
>>> nans, x= np.isnan(y), lambda z: z.nonzero()[0]
W końcu wypłaci dywidendę.
Więc, ilekroć pracujesz z danymi związanymi z Nan, po prostu hermetyzuj wszystkie (new Nan related) potrzebna funkcjonalność, pod pewnymi konkretnymi funkcjami pomocniczymi. Twoja baza kodu będzie bardziej spójna i czytelna, ponieważ podąża za łatwo zrozumiałymi idiomami.
Interpolacja rzeczywiście jest miłym kontekstem, aby zobaczyć, jak odbywa się obsługa NaN, ale podobne techniki są wykorzystywane również w różnych innych kontekstach.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-22 17:14:54
Wymyśliłem ten kod:
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
Drukuje
[ 1. 1.33333333 1.66666667 2. 2. 1. 0. ]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-29 10:19:56
Wystarczy użyć numpy logical and there where, aby zastosować interpolację 1D.
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-17 19:07:56
Może być łatwiej zmienić sposób generowania danych, ale jeśli nie:
bad_indexes = np.isnan(data)
Tworzenie tablicy logicznej wskazującej gdzie są Nan
good_indexes = np.logical_not(bad_indexes)
Utwórz tablicę logiczną wskazującą, gdzie znajduje się obszar dobrych wartości
good_data = data[good_indexes]
Ograniczona wersja oryginalnych danych z wyłączeniem Nan
interpolated = np.interp(bad_indexes.nonzero(), good_indexes.nonzero(), good_data)
Uruchom wszystkie złe indeksy przez interpolację
data[bad_indexes] = interpolated
Zastąp oryginalne dane wartościami interpolowanymi.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-29 10:10:58
Lub bazując na odpowiedzi Winstona
def pad(data):
bad_indexes = np.isnan(data)
good_indexes = np.logical_not(bad_indexes)
good_data = data[good_indexes]
interpolated = np.interp(bad_indexes.nonzero()[0], good_indexes.nonzero()[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
Wynik
[[ 1. 20. 300.]
[ 2. 30. 400.]
[ 3. 40. 500.]]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 12:52:46
Dla danych dwuwymiarowych, SciPy griddata
działa dość dobrze dla mnie:
>>> 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]])
Używam go na obrazach 3D, operując na plasterkach 2D (4000 plasterków 350x350). Cała operacja trwa jeszcze około godziny : /
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-17 13:23:40
Bazując na odpowiedzi autorstwa Bryana Woodsa , zmodyfikowałem jego kod, aby również konwertować listy składające się tylko z NaN
na listę zer:
def fill_nan(A):
'''
interpolate to fill nan values
'''
inds = np.arange(A.shape[0])
good = np.where(np.isfinite(A))
if len(good[0]) == 0:
return np.nan_to_num(A)
f = interp1d(inds[good], A[good], bounds_error=False)
B = np.where(np.isfinite(A), A, f(inds))
return B
Prosty dodatek, mam nadzieję, że komuś się przyda.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:53
Potrzebowałem podejścia, które również wypełniłoby NaN na początku końca danych, czego główna odpowiedź nie wydaje się robić.
Funkcja, którą wymyśliłem, wykorzystuje regresję liniową do wypełnienia NaN. to rozwiązuje mój problem:
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
Oto przykładowy przypadek użycia:
# 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)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-30 09:23:01