tbrugere
tbrugere

Reputation: 1623

Fill nan with nearest neighbor in numpy array

I got this 2D numpy array with missing values. Is there a simple (and reasonably fast) way of filling the nan values with the closest (preferably euclidean distance, but manhattan is ok too) non-nan value? I couldn't find such a function in numpy or scipy…

Upvotes: 6

Views: 3971

Answers (1)

orlp
orlp

Reputation: 117681

Use scipy.interpolate.NearestNDInterpolator.

E.g.:

from scipy.interpolate import NearestNDInterpolator
data = ... # shape (w, h)
mask = np.where(~np.isnan(data))
interp = NearestNDInterpolator(np.transpose(mask), data[mask])
filled_data = interp(*np.indices(data.shape))

Showing it in action (with black as the mask here, image_defect is from from here):

data = image_defect
mask = np.where(~(data == 0))
interp = NearestNDInterpolator(np.transpose(mask), data[mask])
image_result = interp(*np.indices(data.shape))

Then, using the plotting code from scipy: enter image description here

Upvotes: 15

Related Questions