TransformedSpaceKNNImputer¶
yohou.preprocessing.imputation.TransformedSpaceKNNImputer
¶
Bases: BaseTransformer
K-nearest neighbors imputation in a transformed feature space.
Projects the data through an optional transformer before performing KNN imputation. Neighbor search and imputation are both performed in the transformed representation, making the result fundamentally different from composing a transformer and a KNN imputer sequentially in a pipeline.
When transformer=None imputation happens directly on the raw features.
Setting transformer=LagTransformer(lag=k) subsumes a window-based KNN
imputer
because neighbors are now lag-feature vectors, i.e. temporally similar
windows, rather than individual time points. Any projection
(PolynomialFeatures, SplineTransformer, PCA, …) can be used as the
imputation space.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int
|
Number of neighboring samples to use for imputation. |
5
|
weights
|
(uniform, distance)
|
Weight function used in prediction:
|
"uniform"
|
metric
|
nan_euclidean
|
Distance metric for searching neighbors. Only |
"nan_euclidean"
|
transformer
|
BaseTransformer or None
|
An optional yohou transformer used to project the data before KNN
imputation. Must implement |
None
|
Attributes¶
| Name | Type | Description |
|---|---|---|
imputer_ |
sklearn KNNImputer
|
The fitted sklearn KNNImputer instance (fitted in transformed space). |
transformer_ |
BaseTransformer or None
|
A deep-copied and fitted instance of the transformer (or |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> import numpy as np
>>> from yohou.preprocessing import TransformedSpaceKNNImputer
Basic usage (no transformer, raw-feature KNN):
>>> X = pl.DataFrame({
... "time": [datetime(2020, 1, i) for i in range(1, 11)],
... "value": [1.0, 2.0, np.nan, 4.0, 5.0, 6.0, np.nan, 8.0, 9.0, 10.0],
... })
>>> imputer = TransformedSpaceKNNImputer(n_neighbors=3)
>>> imputer.fit(X)
TransformedSpaceKNNImputer(...)
>>> X_imputed = imputer.transform(X)
>>> X_imputed["value"].null_count()
0
With a lag transformer (window-based KNN):
>>> from yohou.preprocessing import LagTransformer
>>> X = pl.DataFrame({
... "time": [datetime(2020, 1, i) for i in range(1, 21)],
... "value": [float(i) for i in range(1, 21)],
... })
>>> imputer = TransformedSpaceKNNImputer(
... n_neighbors=3,
... transformer=LagTransformer(lag=3),
... )
>>> imputer.fit(X)
TransformedSpaceKNNImputer(...)
>>> X_t = imputer.transform(X)
>>> X_t["value_lag_3"].null_count()
0
See Also¶
LagTransformer: Creates lagged features from time series.SimpleTimeImputer: Interpolation-based imputation.SimpleImputer: Simple constant-strategy imputation.
Source Code¶
Show/Hide source
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
Methods¶
__sklearn_tags__()
¶
Get estimator tags.
Returns¶
| Type | Description |
|---|---|
Tags
|
Estimator tags. |
Source Code¶
Show/Hide source
fit(X, y=None, **params)
¶
Fit the imputer, optionally projecting through a transformer first.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input time series with a |
required |
y
|
DataFrame or None
|
Ignored. Present for API compatibility. |
None
|
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
self
|
The fitted imputer instance. |
Source Code¶
Show/Hide source
get_feature_names_out(input_features=None)
¶
Get output feature names for transformation.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
list of str or None
|
Column names of the input features. If |
None
|
Returns¶
| Type | Description |
|---|---|
list of str
|
Output feature names after transformation. |
Source Code¶
Show/Hide source
Tutorials¶
The following example notebooks use this component:
-
How to Handle Missing Data
Data-Features
Compare SimpleTimeImputer, SeasonalImputer, SimpleImputer, and TransformedSpaceKNNImputer on synthetic block and scattered gaps in monthly tourism data.