Nearest neighbors¶
Borrow values from similar rows.
Distance-based imputers that borrow values from similar rows.
KNNImputer
¶
KNNImputer(n_neighbors: int = 5, on_error: OnError = None)
Bases: BaseImputer
Impute missing values using K-nearest neighbors.
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import KNNImputer
>>> df = pd.DataFrame({"a": [1, 2, np.nan, 4], "b": [5, np.nan, 7, 8]})
>>> imputer = KNNImputer(n_neighbors=2)
>>> imputed = imputer.impute(df)
>>> assert not imputed.isna().any().any()
Initialize the imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors to consider. |
5
|
on_error
|
OnError
|
What to do if the model can't be fitted: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If n_neighbors is not a positive integer. |
Source code in src/imputation_methods/neighbors.py
impute
¶
Impute using the fitted KNN strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dataframe is empty or has insufficient data for KNN. |
RuntimeError
|
If KNN imputation fails. |
Source code in src/imputation_methods/neighbors.py
RadiusNeighborsImputer
¶
RadiusNeighborsImputer(radius: float = 1.0, weights: str = 'distance', metric: str = 'euclidean', on_error: OnError = None)
Bases: BaseImputer
Radius-based neighbors imputation using distance threshold.
Imputes using all neighbors within a specified radius rather than a fixed number of neighbors. Adaptive to local density.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
radius
|
float
|
Distance threshold for neighbors. Default: 1.0 |
1.0
|
weights
|
str
|
Weight function ('uniform' or 'distance'). Default: 'distance' |
'distance'
|
metric
|
str
|
Distance metric. Default: 'euclidean' |
'euclidean'
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import RadiusNeighborsImputer
>>> df = pd.DataFrame({
... 'a': [1, 2, np.nan, 4, 5],
... 'b': [2, 4, 6, np.nan, 10]
... })
>>> imputer = RadiusNeighborsImputer(radius=2.0)
>>> imputed = imputer.impute(df)
References
Radius-based neighborhood for adaptive local imputation.
Initialize the radius neighbors imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
radius
|
float
|
Distance threshold |
1.0
|
weights
|
str
|
Weighting function |
'distance'
|
metric
|
str
|
Distance metric |
'euclidean'
|
on_error
|
OnError
|
What to do if the model can't be fitted: |
None
|
Source code in src/imputation_methods/neighbors.py
impute
¶
Impute using radius neighbors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |
Source code in src/imputation_methods/neighbors.py
LocalMeanImputer
¶
Bases: BaseImputer
Local weighted mean imputation based on feature similarity.
Computes weighted average of similar observations, with weights decreasing by distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors to consider. Default: 5 |
5
|
distance_weight_power
|
float
|
Power for distance weighting. Default: 2.0 |
2.0
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> from imputation_methods import LocalMeanImputer
>>> df = pd.DataFrame({'a': [1, 2, np.nan, 4, 5]})
>>> imputer = LocalMeanImputer(n_neighbors=3)
>>> imputed = imputer.impute(df)
References
Locally weighted averaging for smooth imputation.
Initialize the local mean imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighbors |
5
|
distance_weight_power
|
float
|
Power for weighting by distance |
2.0
|
Source code in src/imputation_methods/neighbors.py
impute
¶
Impute using local weighted mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. |