KNNImputer transformer.
K-Nearest Neighbors imputation for missing values.
Classes
Impute missing values using K-Nearest Neighbors.
Each sample's missing values are imputed using the mean value from its k nearest neighbors in the training set.
Constructor
__init__( self, n_neighbors: int = 5, weights: str = 'uniform', columns: Optional[List[int]] = None, )
Overview
KNN imputation is a multivariate strategy that uses the similarity between samples to estimate missing values. It is generally more accurate than simple univariate imputation when features are correlated.
Parameters
n_neighbors
int
= 5
Number of neighboring samples to use for imputation.
weights
{"uniform", "distance"}
= "uniform"
Weight function used in prediction:
- •
"uniform": All neighbors are weighted equally. - •
"distance": Weights neighbors by the inverse of their distance.
columns
list of int
Indices of columns to impute. If
None, all columns are processed.
See Also
Impute using 2 nearest neighbors:
python
>>> from tuiml.preprocessing.imputation import KNNImputer
>>> import numpy as np
>>> X = np.array([[1, 2], [3, 4], [np.nan, 6], [8, 8]])
>>> imputer = KNNImputer(n_neighbors=2)
>>> X_imputed = imputer.fit_transform(X)
Methods
get_parameter_schema
(cls)
fit
(self, X: np.ndarray, y: Optional[np.ndarray]=None, feature_names: Optional[List[str]]=None) -> 'KNNImputer'
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str