Nearest-neighbour imputation¶
For a sample whose view is missing, neighbours are searched among the samples that do have it, using the other views as the search space; the missing block is then the average of those neighbours. Distances are computed per view on standardised features and averaged over the views two samples have in common, so samples sharing a single view remain comparable to samples sharing several.
Nearest-neighbour imputation of missing views.
- class polyview.imputation.knn.KNNViewImputer(*args: Any, **kwargs: Any)¶
Bases:
BaseViewImputerReconstruct a missing view from the nearest samples that have it.
For a sample whose view
vis missing, neighbours are searched among the fit samples that do have viewv, using the other views as the search space; the missing block is then the (weighted) average of those neighbours’ viewv. UnlikeSimpleViewImputer, this exploits the fact that views of the same sample are correlated: two samples that look alike in the views they share usually look alike in the view one of them is missing.Distances are computed per view on standardised features and averaged over the views the two samples have in common, so samples that share only one view are still comparable to samples that share three.
- Parameters:
n_neighbors (int, default=5) – Number of donors averaged per missing view. Clipped to the number of available donors.
weights ({"uniform", "distance"}, default="uniform") –
"uniform"averages the donors equally;"distance"weights them by the inverse of their distance, so closer samples count more.n_views (int or None, default=None) – Expected number of views.
Noneaccepts any count.
- missing_view_mask_¶
Truewhere a whole view was missing at fit time.- Type:
ndarray of bool of shape (n_samples, n_views)
- missing_rate_¶
Fraction of fit samples for which each view was missing.
- Type:
ndarray of shape (n_views,)
- column_fill_¶
Per-view column means used when no donor can be found.
- Type:
list of ndarray
Notes
Donors for view
vare the fit samples whose viewvis fully observed. When a view has no such sample, or when a sample shares no view with any donor, the column mean is used instead.With a single view there is nothing left to measure distances with, and every sample falls back to the column mean; use
sklearn.impute.KNNImputerfor within-view imputation.Examples
>>> import numpy as np >>> from polyview.imputation import KNNViewImputer >>> X1 = np.array([[0.0], [0.1], [5.0], [5.1]]) >>> X2 = np.array([[1.0], [1.2], [9.0], [np.nan]]) >>> filled = KNNViewImputer(n_neighbors=1).fit_transform([X1, X2]) >>> filled[1].ravel() # nearest sample in view 0 is row 2 array([1. , 1.2, 9. , 9. ])